gonja
is a pure go
implementation of the Jinja template engine. It aims to be mostly compatible with the original python
implementation but also provides additional features to compensate the lack of python
scripting capabilities.
Install/update using go get
:
go get github.com/MarioJim/gonja
package main
import (
"fmt"
"github.com/MarioJim/gonja"
)
func main() {
tpl, err := gonja.FromString("Hello {{ name | capitalize }}!")
if err != nil {
panic(err)
}
out, err := tpl.Execute(gonja.Context{"name": "bob"})
if err != nil {
panic(err)
}
fmt.Println(out) // Prints: Hello Bob!
}
- For a details on how the template language works, please refer to the Jinja documentation ;
gonja
API documentation is available on godoc ;filters
: please refer todocs/filters.md
;statements
: please take a look atdocs/statements.md
;tests
: please seedocs/tests.md
;globals
: please browse throughdocs/globals.md
.
- format:
format
does not take Python's string format syntax as a parameter, instead it takes Go's. Essentially{{ 3.14|stringformat:"pi is %.2f" }}
isfmt.Sprintf("pi is %.2f", 3.14)
. - escape / force_escape: Unlike Jinja's behavior, the
escape
-filter is applied immediately. Therefore there is no need for aforce_escape
-filter yet.
A massive thank you to the original author @noirbizarre for doing the initial work in https://github.com/noirbizarre/gonja which this project was forked from.