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') %}
{% 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:
Name: {{ user.name }}
Email: {{ user.email }}
Role: {{ user.role }}
{% 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:
site_name: {{ site_name }}
current_user: {{ current_user }}
user.name: {{ user.name }}
{# 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.