gonja: github.com/noirbizarre/gonja Index | Files | Directories

package gonja

import "github.com/noirbizarre/gonja"

A jinja like template-engine

Blog posts about gonja (including introduction and migration): https://www.florian-schlachter.de/?tag=gonja

Complete documentation on the template language: https://docs.djangoproject.com/en/dev/topics/templates/

Try out gonja live in the gonja playground: https://www.florian-schlachter.de/gonja/

Make sure to read README.md in the repository as well.

A tiny example with template strings:

(Snippet on playground: https://www.florian-schlachter.de/gonja/?id=1206546277)

// Compile the template first (i. e. creating the AST)
tpl, err := gonja.FromString("Hello {{ name|capfirst }}!")
if err != nil {
// Now you can render the template with the given
// gonja.Context how often you want to.
out, err := tpl.Execute(gonja.Context{"name": "fred"})
if err != nil {
fmt.Println(out) // Output: Hello Fred!


Package Files

config.go context.go doc.go environment.go gonja.go version.go


const VERSION = "0.1.0"


var (
    // DefaultLoader is being used by the DefaultSet.
    DefaultLoader = loaders.MustNewFileSystemLoader("")

    // DefaultEnv is an environment created for quick/standalone template rendering.
    DefaultEnv = NewEnvironment(config.DefaultConfig, DefaultLoader)

    // Methods on the default set
    FromString = DefaultEnv.FromString
    FromBytes  = DefaultEnv.FromBytes
    FromFile   = DefaultEnv.FromFile
    FromCache  = DefaultEnv.FromCache

    // Globals for the default set
    Globals = DefaultEnv.Globals
var NewConfig = config.NewConfig

func Must Uses

func Must(tpl *exec.Template, err error) *exec.Template

Must panics, if a Template couldn't successfully parsed. This is how you would use it:

var baseTemplate = gonja.Must(gonja.FromFile("templates/base.html"))

type Config Uses

type Config config.Config

type Context Uses

type Context map[string]interface{}

type Environment Uses

type Environment struct {
    Loader loaders.Loader

    Cache      map[string]*exec.Template
    CacheMutex sync.Mutex

func NewEnvironment Uses

func NewEnvironment(cfg *config.Config, loader loaders.Loader) *Environment

func (*Environment) CleanCache Uses

func (env *Environment) CleanCache(filenames ...string)

CleanCache cleans the template cache. If filenames is not empty, it will remove the template caches of those filenames. Or it will empty the whole template cache. It is thread-safe.

func (*Environment) FromBytes Uses

func (env *Environment) FromBytes(tpl []byte) (*exec.Template, error)

FromBytes loads a template from bytes and returns a Template instance.

func (*Environment) FromCache Uses

func (env *Environment) FromCache(filename string) (*exec.Template, error)

FromCache is a convenient method to cache templates. It is thread-safe and will only compile the template associated with a filename once. If Environment.Debug is true (for example during development phase), FromCache() will not cache the template and instead recompile it on any call (to make changes to a template live instantaneously).

func (*Environment) FromFile Uses

func (env *Environment) FromFile(filename string) (*exec.Template, error)

FromFile loads a template from a filename and returns a Template instance.

func (*Environment) FromString Uses

func (env *Environment) FromString(tpl string) (*exec.Template, error)

FromString loads a template from string and returns a Template instance.

func (*Environment) GetTemplate Uses

func (env *Environment) GetTemplate(filename string) (*exec.Template, error)



Package gonja imports 7 packages (graph) and is imported by 1 packages. Updated 2020-07-03. Refresh now. Tools for package owners.