Skip to content
zipreportPublic

About

jinja2 golang library

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

347 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Miya Engine

Go Reference Go Report Card Test License: MIT Go Version

A high-performance Jinja2-compatible template engine implementation in Go.

Documentation | Go Reference

Features

  • 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

Installation

go get github.com/zipreport/miya

Quick Start

package 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!
}

Supported Features

Core Features

  • 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

Built-in Filters

  • String: capitalize, lower, upper, title, trim, replace
  • HTML: escape, safe, urlencode
  • Collections: first, last, length, join, sort, reverse
  • Numbers: abs, round, int, float

Advanced Examples

List/Dict Comprehensions

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.48

Filter Blocks

Apply 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- BANANA

Chained Filters

template := `{% filter trim|upper|reverse %}   hello world   {% endfilter %}`
result, _ := env.RenderString(template, ctx)
// Output: DLROW OLLEH

Do Statements

Execute expressions for side effects:

template := `{% do complex_calculation %}Result: {{ result }}`

Memory Management

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()

Documentation

Full documentation is available at zipreport.github.io/miya.

More Examples

Check out the examples/ directory for comprehensive usage examples.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE for details.

About

jinja2 golang library

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages