gonja

package module
v0.0.0-...-6f1b420 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: MIT Imports: 7 Imported by: 0

README

Jingo

Documentation

Overview

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 {
    panic(err)
}
// 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 {
    panic(err)
}
fmt.Println(out) // Output: Hello Fred!

Index

Constants

View Source
const VERSION = "0.1.0"

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)

Directories

Path Synopsis
ext

Jump to

Keyboard shortcuts

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