A high-performance Jinja2-compatible template engine implementation in Go.
- Jinja2 Compatibility: Parse and render Jinja2 templates with familiar syntax
- High Performance: Optimized for speed with template compilation and caching
- Template caching: compiled templates are cached by the environment with no size limit or expiry;
Release()drops a one-off template from the cache - Template Inheritance: extends, blocks,
super()and includes (see the limitations page for the known gaps) - Rich Filter System: Built-in filters plus support for custom filters
- Flexible Loading: Load templates from filesystem, memory, or embedded resources
- Security: Auto-escaping enabled by default to prevent XSS
- Developer Friendly: Clear error messages with line numbers and debugging support
go get github.com/zipreport/miyapackage main
import (
"fmt"
"log"
"github.com/zipreport/miya"
)
func main() {
env := miya.NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
log.Fatal(err)
}
ctx := miya.NewContext()
ctx.Set("name", "World")
output, err := tmpl.Render(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(output) // Output: Hello World!
}- Variable substitution:
{{ variable }} - Control structures:
{% if %},{% for %}, etc. - Template inheritance:
{% extends %},{% block %} - Includes:
{% include %} - Macros:
{% macro %} - Filters:
{{ value|upper|trim }} - List/Dict comprehensions:
{{ [x*2 for x in items] }} - Filter blocks:
{% filter upper %}content{% endfilter %} - Do statements:
{% do expression %} - Comments:
{# comment #} - Whitespace control:
{%- -%} - Custom delimiters
- String:
capitalize,lower,upper,title,trim,replace - HTML:
escape,safe,urlencode - Collections:
first,last,length,join,sort,reverse - Numbers:
abs,round,int,float
Create collections with concise syntax:
template := `
Active users: {{ [user.name for user in users if user.active] }}
User lookup: {{ {user.id: user.name for user in users} }}
Cart total: ${{ [item.price * item.qty for item in cart]|sum }}
`
ctx := miya.NewContext()
ctx.Set("users", []map[string]interface{}{
{"id": 1, "name": "Alice", "active": true},
{"id": 2, "name": "Bob", "active": false},
{"id": 3, "name": "Charlie", "active": true},
})
ctx.Set("cart", []map[string]interface{}{
{"price": 10.99, "qty": 2},
{"price": 5.50, "qty": 1},
})
result, _ := env.RenderString(template, ctx)
// Output: Active users: [Alice, Charlie]
// User lookup: {1: Alice, 2: Bob, 3: Charlie}
// Cart total: $27.48Apply filters to entire blocks of content:
template := `{% filter upper %}
Hello {{ name }}!
Your items:
{% for item in items %}
- {{ item }}
{% endfor %}
{% endfilter %}`
ctx := miya.NewContext()
ctx.Set("name", "Alice")
ctx.Set("items", []string{"apple", "banana"})
result, _ := env.RenderString(template, ctx)
// Output: HELLO ALICE!\nYOUR ITEMS:\n- APPLE\n- BANANAtemplate := `{% filter trim|upper|reverse %} hello world {% endfilter %}`
result, _ := env.RenderString(template, ctx)
// Output: DLROW OLLEHExecute expressions for side effects:
template := `{% do complex_calculation %}Result: {{ result }}`Compiled templates are cached by the environment, by name for GetTemplate and by source for FromString, and the cache does not expire. If you compile many distinct one-off templates, call tmpl.Release() when done: it removes that template from the cache, so it can be garbage collected once you drop it. The template stays usable; compiling the same source again re-parses it. ClearCache() drops everything.
tmpl, _ := env.FromString("Hello {{ name }}!")
output, _ := tmpl.Render(ctx)
// drop this one-off template from the environment's cache
tmpl.Release()Full documentation is available at zipreport.github.io/miya.
Check out the examples/ directory for comprehensive usage examples.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE for details.