🐚 🖼️ TuskLang Bash @render Function Guide
🖼️ TuskLang Bash @render Function Guide
"We don't bow to any king" – Rendering is your configuration's creative engine.
The @render function in TuskLang is your dynamic templating and content generation powerhouse, enabling you to render templates, generate configuration files, and produce dynamic content directly within your configuration files. Whether you're building config templates, generating emails, or automating documentation, @render gives you the expressive power to create and customize output on the fly.
🎯 What is @render?
The @render function provides dynamic template rendering in TuskLang. It offers: - Template rendering - Render text, config, or code templates with variables - Multi-format support - Render INI, JSON, YAML, HTML, Markdown, and more - Variable interpolation - Inject variables, arrays, and objects into templates - Conditional logic - Use @if, @for, and other logic in templates - Reusable templates - Reference and reuse templates across configs📝 Basic @render Syntax
Simple Template Rendering
[simple_render]
Render a string template with variables
$user: "Alice"
$greeting_template: "Hello, {name}! Welcome to TuskLang."
greeting: @render($greeting_template, {"name": $user})Render with inline template
date_message: @render("Today is {date}", {"date": @date("Y-m-d")})
Rendering with Arrays and Objects
[array_render]
Render a list of items
$items: ["apple", "banana", "cherry"]
$list_template: "- {item}"
rendered_list: @array.map($items, @render($list_template, {"item": item}))[object_render]
Render object properties
$user: {"name": "Bob", "email": "bob@example.com"}
$user_template: "Name: {name}\nEmail: {email}"
user_info: @render($user_template, $user)
Multi-Line and File Templates
[multiline_render]
Render a multi-line template
$multi_template: """
Dear {name},Your account has been created.
Login: {email}
Best,
TuskLang Team
"""
$recipient: {"name": "Charlie", "email": "charlie@example.com"}
welcome_email: @render($multi_template, $recipient)
[file_template_render]
Render a template from a file
$template_file: @file.read("/etc/app/email-template.txt")
$email_data: {"name": "Dana", "email": "dana@example.com"}
rendered_email: @render($template_file, $email_data)
🚀 Quick Start Example
#!/bin/bash
source tusk-bash.shcat > render-quickstart.tsk << 'EOF'
[template_demo]
Render a greeting
$user: "Eve"
greeting_template: "Hello, {name}!"
greeting: @render($greeting_template, {"name": $user})Render a config file
$config_template: """
[database]
host = {host}
port = {port}
user = {user}
password = {password}
"""$db_data: {
"host": "localhost",
"port": 5432,
"user": "eve",
"password": "secretpass"
}
rendered_config: @render($config_template, $db_data)
Render a Markdown report
$report_template: """
Daily Report
Date: {date}
User: {user}
Summary
- Tasks completed: {tasks}
- Issues: {issues}
"""$report_data: {
"date": @date("Y-m-d"),
"user": $user,
"tasks": 5,
"issues": 0
}
rendered_report: @render($report_template, $report_data)
EOF
config=$(tusk_parse render-quickstart.tsk)
echo "=== Template Demo ==="
echo "Greeting: $(tusk_get "$config" template_demo.greeting)"
echo ""
echo "Rendered Config:"
echo "$(tusk_get "$config" template_demo.rendered_config)"
echo ""
echo "Rendered Report:"
echo "$(tusk_get "$config" template_demo.rendered_report)"
🔗 Real-World Use Cases
1. Configuration File Generation
[config_generation]
Render dynamic configuration files
$config_template: """
[server]
host = {host}
port = {port}
mode = {mode}
"""$server_data: {
"host": @env("SERVER_HOST", "127.0.0.1"),
"port": @env("SERVER_PORT", "8080"),
"mode": @env("SERVER_MODE", "production")
}
rendered_config: @render($config_template, $server_data)
@file.write("/etc/app/generated-config.tsk", $rendered_config)
2. Automated Email and Notification Templates
[email_templates]
Render email templates
$email_template: """
Subject: Welcome, {name}!Dear {name},
Thank you for joining TuskLang. Your username is {username}.
Best,
TuskLang Team
"""
$user_data: {
"name": @env("USER_NAME", "Friend"),
"username": @env("USER_LOGIN", "user123")
}
welcome_email: @render($email_template, $user_data)
3. Dynamic HTML/Markdown Generation
[html_generation]
Render HTML templates
$html_template: """
<html>
<head><title>{title}</title></head>
<body>
<h1>{heading}</h1>
<p>{content}</p>
</body>
</html>
"""$html_data: {
"title": "Welcome Page",
"heading": "Hello, World!",
"content": "This page was generated by TuskLang."
}
rendered_html: @render($html_template, $html_data)
[markdown_generation]
Render Markdown templates
$md_template: """
{title}
{body}
"""
$md_data: {
"title": "Release Notes",
"body": "- Feature A added\n- Bug B fixed\n- Performance improved"
}
rendered_markdown: @render($md_template, $md_data)
4. Batch Rendering with Arrays
[batch_rendering]
Render a list of user summaries
$users: [
{"name": "Alice", "email": "alice@example.com"},
{"name": "Bob", "email": "bob@example.com"},
{"name": "Charlie", "email": "charlie@example.com"}
]$user_template: "- {name} <{email}>"
user_summaries: @array.map($users, @render($user_template, item))
🧠 Advanced @render Patterns
Conditional and Loop Logic in Templates
[advanced_templates]
Use @if and @for in templates
$tasks: ["Write docs", "Review PRs", "Deploy app"]
$task_template: "- {task}"$report_template: """
Task Report
{#for task in tasks}
{task}
{#/for}
{#if issues > 0}
Issues found: {issues}
{#/if}
"""
$report_data: {
"tasks": @array.map($tasks, @render($task_template, {"task": item})),
"issues": 0
}
rendered_report: @render($report_template, $report_data)
Template Inheritance and Includes
[template_inheritance]
Base and child templates
$base_template: """
<html>
<head><title>{title}</title></head>
<body>
{content}
</body>
</html>
"""$page_template: """
{#include base_template}
{content}
"""
$page_data: {
"title": "Dashboard",
"content": "<h1>Welcome to the dashboard!</h1>"
}
rendered_page: @render($page_template, $page_data)
File-Based Batch Rendering
[file_batch_render]
Render multiple files from templates
$users: @array.from_query("SELECT name, email FROM users WHERE active = 1")
$user_template: @file.read("/etc/app/user-template.txt")rendered_files: @array.map($users, @render($user_template, item))
@array.for_each(rendered_files, @file.write("/var/reports/" + item.name + ".txt", item))
🛡️ Security & Performance Notes
- Input validation: Sanitize variables before rendering to prevent injection - Template escaping: Escape special characters in HTML/JS templates - Performance: Cache rendered templates for repeated use - File permissions: Restrict write access to generated files - Resource usage: Monitor memory/CPU for large batch renders🐞 Troubleshooting
- Missing variables: Ensure all template variables are provided - Syntax errors: Validate template syntax and delimiters - Encoding issues: Use UTF-8 for multi-language templates - Performance: Cache or batch large renders - File write errors: Check file permissions and paths💡 Best Practices
- Use descriptive templates: Clearly document template variables - Sanitize inputs: Prevent injection in rendered output - Cache templates: Improve performance for repeated renders - Modularize templates: Use includes and inheritance for maintainability - Test output: Validate rendered files and content - Restrict file writes: Limit where rendered files can be written🔗 Cross-References
- @ Operator Introduction - @string Function - File Operations - Multiline Values - Templates and Includes---
Master @render in TuskLang and unleash creative, dynamic configuration generation. 🖼️