-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
semihalev edited this page Mar 11, 2025
·
1 revision
The Twig repository includes several example applications demonstrating various features:
A basic example showing how to use Twig templates:
// From examples/simple/main.go
package main
import (
"fmt"
"github.com/semihalev/twig"
"os"
)
func main() {
// Create a Twig engine
engine := twig.New()
// Load templates from memory
template := "Hello, {{ name }}\!"
engine.AddTemplateString("greeting", template)
// Render the template
context := map[string]interface{}{
"name": "World",
}
result, err := engine.Render("greeting", context)
if err \!= nil {
fmt.Println("Error:", err)
return
}
fmt.Println(result) // Output: Hello, World\!
}
// From examples/development_mode/main.go
package main
import (
"fmt"
"github.com/semihalev/twig"
"os"
)
func main() {
// Create a Twig engine with development mode enabled
engine := twig.New()
engine.SetDevelopmentMode(true)
// Add a template loader
loader := twig.NewFileSystemLoader([]string{"./templates"})
engine.RegisterLoader(loader)
// Render a template
context := map[string]interface{}{
"name": "Developer",
}
// Templates will auto-reload if changed
result, err := engine.Render("hello.twig", context)
if err \!= nil {
fmt.Println("Error:", err)
return
}
fmt.Println(result)
}
Example showing how to create custom Twig extensions:
// From examples/custom_extensions/main.go
package main
import (
"fmt"
"github.com/semihalev/twig"
"strings"
)
func main() {
// Create a Twig engine
engine := twig.New()
// Register a custom extension
engine.RegisterExtension("text_tools", func(ext *twig.CustomExtension) {
// Add a filter to count words
ext.Filters["word_count"] = func(value interface{}, args ...interface{}) (interface{}, error) {
str, ok := value.(string)
if \!ok {
return 0, nil
}
return len(strings.Fields(str)), nil
}
// Add a function to generate Lorem Ipsum text
ext.Functions["lorem"] = func(args ...interface{}) (interface{}, error) {
count := 5
if len(args) > 0 {
if c, ok := args[0].(int); ok {
count = c
}
}
return strings.Repeat("Lorem ipsum dolor sit amet. ", count), nil
}
})
// Use the custom extensions in a template
template := `
The following text has {{ text|word_count }} words:
{{ text }}
Generated text:
{{ lorem(3) }}
`
engine.AddTemplateString("example", template)
// Render the template
context := map[string]interface{}{
"text": "This is an example of a custom filter in action.",
}
result, err := engine.Render("example", context)
if err \!= nil {
fmt.Println("Error:", err)
return
}
fmt.Println(result)
}
Example showing how to use macros for reusable UI components:
// From examples/macros/main.go
package main
import (
"fmt"
"github.com/semihalev/twig"
"os"
)
func main() {
// Create a new Twig engine
engine := twig.New()
// Create template with macros
macrosTemplate := `
{# Define macros in a separate template #}
{% macro input(name, value = '', type = 'text', size = 20) %}
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}">
{% endmacro %}
{% macro label(text, for = '') %}
<label{% if for %} for="{{ for }}"{% endif %}>{{ text }}</label>
{% endmacro %}
`
// Create a template that imports and uses macros
mainTemplate := `
{% import "macros.twig" as forms %}
<form>
<div class="form-row">
{{ forms.label('Username', 'username') }}
{{ forms.input('username', user.username) }}
</div>
<div class="form-row">
{{ forms.input('submit', 'Submit', 'submit') }}
</div>
</form>
`
// Register templates
engine.RegisterString("macros.twig", macrosTemplate)
engine.RegisterString("main.twig", mainTemplate)
// Create context with user data
context := map[string]interface{}{
"user": map[string]interface{}{
"username": "johndoe",
},
}
// Render the template
err := engine.RenderTo(os.Stdout, "main.twig", context)
if err \!= nil {
fmt.Printf("Error rendering template: %v\n", err)
}
}
More examples can be found in the examples/
directory of the repository:
-
examples/compiled_templates/
- Shows how to compile and use compiled templates -
examples/macros/
- Demonstrates the use of macros in templates with nested and imported examples -
examples/development_mode/
- Shows how to use the development mode for template auto-reloading -
examples/simple/
- Basic usage examples for quick reference