package main import ( "fmt" "os" "github.com/semihalev/twig" ) func main() { // Create a new Twig engine engine := twig.New() // Template with _self reference for macros selfRefTemplate := ` {% macro input(name, value = '') %} {% endmacro %} {% macro form(action, method = 'post') %}
{# Use _self to reference macros in the same template #} {{ _self.input('username', 'john') }}
{% endmacro %}

Example of using _self to reference macros in the same template:

{{ _self.form('/submit') }}
` // Template demonstrating macro scope scopeTemplate := ` {% set name = 'Global' %} {% macro greet(name = 'Default') %} {# This only sees the 'name' parameter, not the global 'name' #}
Hello, {{ name }}!
{% endmacro %}

Example of macro variable scope:

Using default parameter: {{ greet() }}

Using provided parameter: {{ greet('Local') }}

Global variable outside macro: {{ name }}

` // Template showing global context access vs macro scope contextTemplate := ` {% set site_name = 'Twig Examples' %} {% set current_user = 'admin' %} {% macro show_user_info(user) %}

User Information:

{% endmacro %} {% macro greet(username) %}

Hello, {{ username }}!

Welcome to {{ site_name }}

{# This won't work - site_name is not in scope #}
{% endmacro %}

Example of macro scope vs global scope:

These variables are available in the global scope:

{# Passing specific values to the macro #} {{ show_user_info(user) }} {# This will show that site_name is not accessible in macro scope #} {{ greet(user.name) }}
` // Template with macro library organization libraryTemplate := ` {# This template simulates a component library with multiple macros #} {% macro panel(title, content, type = 'default') %}
{{ title }}
{{ content }}
{% endmacro %} {% macro alert(message, type = 'info') %}
{{ message }}
{% endmacro %} {% macro badge(text, color = 'blue') %} {{ text }} {% endmacro %} ` // Template that uses the library useLibraryTemplate := ` {% import "library.twig" as ui %}

Example of organizing macros in libraries:

{{ ui.panel('User Profile', 'Welcome back, ' ~ user.name, 'primary') }} {{ ui.alert('Your account has been verified!', 'success') }}

Status: {{ ui.badge('Active', 'green') }}

` // Template demonstrating selective import (workaround for "from ... import") fromImportTemplate := ` {# Current implementation doesn't fully support "from X import Y" syntax directly #} {# This example just shows how to use the namespace with "import" #} {% import "library.twig" as lib %}

Example of selective macro usage:

{# Use specific macros from the library, but not all of them #} {{ lib.panel('Quick Stats', 'Visitors today: 1,234', 'info') }} {{ lib.alert('New message received!', 'warning') }} {# We don't use the badge macro to simulate selective import #}

Note: Full "from ... import" selective import syntax is on the roadmap

` // Template with performance optimization techniques optimizationTemplate := `

Macro Performance Optimization Techniques:

{# 1. Use imported macros for better performance #}

Technique #1: Use imported macros when possible

Imported macros perform better than direct usage.

{% import "forms.twig" as forms %}
{{ forms.input('username') }}
{# 2. Group related macros in separate files #}

Technique #2: Group related macros in separate files

This helps with organization and caching efficiency.

forms.twig - Input and form macros
layout.twig - Layout components
widgets.twig - UI widgets
{# 3. Import once, use multiple times #}

Technique #3: Import once, use multiple times

Import at the top of your template to reuse throughout.

{% import "macros.twig" as m %}
{{ m.widget1() }}
{{ m.widget2() }}
{{ m.widget3() }}
{# 4. Simple macros with focused responsibility #}

Technique #4: Keep macros simple and focused

Smaller, focused macros are easier to optimize and cache.

{% macro avatar(user) %}
  <img src="{{ user.avatar }}" alt="{{ user.name }}">
{% endmacro %}

{% macro userinfo(user) %}
  <div>{{ avatar(user) }} {{ user.name }}</div>
{% endmacro %}
` // Register templates templates := map[string]string{ "self_ref.twig": selfRefTemplate, "scope.twig": scopeTemplate, "context.twig": contextTemplate, "library.twig": libraryTemplate, "use_library.twig": useLibraryTemplate, "from_import.twig": fromImportTemplate, "optimization.twig": optimizationTemplate, } for name, content := range templates { err := engine.RegisterString(name, content) if err != nil { fmt.Printf("Error registering template %s: %v\n", name, err) return } } // Create context for templates context := map[string]interface{}{ "user": map[string]interface{}{ "name": "John Doe", "email": "john@example.com", "role": "Administrator", }, "items": []string{"Item 1", "Item 2", "Item 3"}, "settings": map[string]interface{}{ "theme": "dark", "public": true, }, } // Render each template fmt.Println("===== ADVANCED MACRO EXAMPLES =====") for _, name := range []string{"self_ref.twig", "scope.twig", "context.twig", "use_library.twig", "from_import.twig", "optimization.twig"} { fmt.Printf("\n----- %s -----\n\n", name) err := engine.RenderTo(os.Stdout, name, context) if err != nil { fmt.Printf("Error rendering template %s: %v\n", name, err) continue } fmt.Println() } }