gonja

package module
v1.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 8 Imported by: 1

README

gonja

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.

Usage

As a library

Install/update using go get:

go get github.com/MarioJim/gonja

Example

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

Documentation

Limitations

  • 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" }} is fmt.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 a force_escape-filter yet.

Tribute

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.

Documentation

Index

Constants

View Source
const VERSION = "1.5.4"

Variables

View Source
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
)
View Source
var NewConfig = config.NewConfig

Functions

func Must

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

Types

type Config

type Config config.Config

type Context

type Context map[string]interface{}

type Environment

type Environment struct {
	*exec.EvalConfig
	Loader loaders.Loader

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

func NewEnvironment

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

func (*Environment) CleanCache

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

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

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

func (*Environment) FromCache

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

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

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

func (*Environment) FromString

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

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

func (*Environment) GetTemplate

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

func (*Environment) Path

func (env *Environment) Path(path string) (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL