gonja is a pure go implementation of the Jinja template engine. It aims to be as compatible as possible with the original python implementation.
Install or update using go get:
go get github.com/nikolalohinski/gonja/v2
This gonja library has been packaged as a terraform provider. For more information, please refer to the dedicated documentation.
package main
import (
"os"
"fmt"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
)
func main() {
template, err := gonja.FromString("Hello {{ name | capitalize }}!")
if err != nil {
panic(err)
}
data := exec.NewContext(map[string]interface{}{
"name": "bob",
})
if err = template.Execute(os.Stdout, data); err != nil { // Prints: Hello Bob!
panic(err)
}
}- For details on how the Jinja template language works, please refer to the Jinja documentation ;
- gonja API documentation is available on godoc ;
- filters: please refer to
docs/filters.md; - control structures: please take a look at
docs/control_structures.md; - tests: please see
docs/tests.md; - global functions: please browse through
docs/global_functions.md. - global variables: please open
docs/global_variables.md. - methods: please take a peek at
docs/methods.md.
As this project now aims to reproduce the behavior of the python Jinja engine as closely as possible, some backwards incompatible changes have been made from the initial draft and need to be taken into account when upgrading from v1.X.X. Moreover, please do note that v1.X.X versions are not maintained.
The following steps can be used as general guidelines to migrate from v1 to v2:
- All references to
gonjaneed to be changed from"github.com/nikolalohinski/gonja"to"github.com/nikolalohinski/gonja/v2" - The following top level global variables/functions have been removed/updated and need to be adjusted accordingly:
DefaultEnvfunction is now calledDefaultEnvironmentand its properties have changed. See gonja.go and exec/environment.go for detailsFromCachefunction has been removed as caching logic was removed. If required, it can be done by implementing a customLoader(seeloaders/loader.go)Globalsis now referred to asDefaultContext
- What was called a
Statementis now referred to asControlStructureto be closer topython's Jinja glossary and may require changes in consumer code - What was called
Globalsis now calledGlobalFunctionsto be closer topython's Jinja glossary and may require changes in consumer code - All non-
pythonbuilt-ins have been removed fromgonja. They have been moved to theterraform-provider-jinjacode base. They can be brought back as needed by adding thegithub.com/NikolaLohinski/terraform-provider-jinja/libdependency, and updating the global variables defined inbuiltins/with the available methods for each (seeexec/environment.gofor details) - The
Executemethod of the*exec.Templateobject now requires aio.Writerto be passed, to be closer to Golang'stemplatepackage interface. However, theExecuteToStringmethod now exists and behaves exactly as theExecutemethod used to, so it can be used as drop-in replacement.
- format:
formatdoes not takepython'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_escapefilter - Only subsets of native
pythontypes (bool,int,float,str,dictandlist) methods have been re-implemented in Go and can slightly differ from the original ones
Please read through the contribution guidelines before diving into any work.
- Install go
>= 1.21by following the official documentation ; - Install
ginkgoby any means you see fit.
The unit tests can be run using:
ginkgo run -p ./...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.