gonja

package
v0.0.0-...-b160839 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

A jinja like template-engine for Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultEnv is an environment created for quick/standalone template
	// rendering. It uses the NullLoader, which means that you can't use
	// `include` or `extends` to load other templates.
	DefaultEnv = NewEnvironment()

	// FromString is a quick way to parse a template from a string. The template
	// doesn't allow any includes. If you want to use includes, create a custom
	// environment with an appropriate loader.
	FromString = DefaultEnv.FromString
	// FromBytes is a quick way to parse a template from a byte slice. The
	// template doesn't allow any includes. If you want to use includes, create
	// a custom environment with an appropriate loader.
	FromBytes = DefaultEnv.FromBytes
)

convenient interface to create a new UndefinedFunc

View Source
var (
	NullLoader                      = loaders.NewNullLoader
	FileSystemLoader                = loaders.NewFileSystemLoader
	MustFileSystemLoader            = loaders.MustNewFileSystemLoader
	FileSystemLoaderWithOptions     = loaders.NewFileSystemLoaderWithOptions
	MustFileSystemLoaderWithOptions = loaders.MustNewFileSystemLoaderWithOptions
	CachedLoader                    = loaders.NewCachedLoader
)

convenient interface to create a new Loaders

Functions

func FromFile

func FromFile(path string) (*exec.Template, error)

FromFile is a quick way to parse a template from a file. The template doesn't allow any includes. If you want to use includes, create a custom environment with an appropriate loader.

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 tpl = gonja.Must(gonja.FromFile("templates/base.html"))

Types

type Environment

type Environment struct {
	*exec.EvalConfig
	// contains filtered or unexported fields
}

Environment is the core component of the Gonja template engine. It contains important shared variables like configuration, filters, tests, globals and others.

func NewEnvironment

func NewEnvironment(options ...Option) *Environment

NewEnvironment creates a new Environment.

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

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

FromFile loads a template from a path and returns a Template instance. It uses the configured loader, so make sure you provided a loader that will find and load the template.

func (*Environment) FromString

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

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

type Option

type Option func(*Environment)

Option is a function that can be used to configure the gonja template engine.

func OptAutoescape

func OptAutoescape() Option

OptAutoescape enables the XML/HTML autoescaping feature. It is disabled by default.

func OptBlockEndString

func OptBlockEndString(s string) Option

OptBlockEndString marks the end of a block. Defaults to '%}'.

func OptBlockStartString

func OptBlockStartString(s string) Option

OptBlockStartString marks the beginning of a block. Defaults to '{%'

func OptCommentEndString

func OptCommentEndString(s string) Option

OptCommentEndString marks the end of a comment. Defaults to '#}'.

func OptCommentStartString

func OptCommentStartString(s string) Option

OptCommentStartString marks the beginning of a comment. Defaults to '{#'.

func OptKeepTrailingNewline

func OptKeepTrailingNewline() Option

OptKeepTrailingNewline enables the preservation of trailing newline when rendering templates. It is disabled by default, which causes a single newline, if present, to be stripped from the end of the template.

func OptLineCommentPrefix

func OptLineCommentPrefix(s string) Option

OptLineCommentPrefix will be used as prefix for line based comments, if given and a string.

func OptLineStatementPrefix

func OptLineStatementPrefix(s string) Option

OptLineStatementPrefix will be used as prefix for line based statements, if given and a string.

func OptLoader

func OptLoader(loader loaders.Loader) Option

OptLoader sets the loader that will be used to load templates by name. To set a cached filesystem loader for example, use the following code:

env := gonja.NewEnvironment(
    gonja.OptLoader(gonja.CachedLoader(gonja.MustFilesystemLoader("path/to/templates"))),
)

func OptLstripBlocks

func OptLstripBlocks() Option

OptLstripBlocks enables the strip of leading spaces and tabs from the start of a line to a block. Disabled by default.

func OptNewlineSequence

func OptNewlineSequence(s string) Option

OptNewlineSequence defines the sequence that starts a newline. Must be one of '\r', '\n' or '\r\n'. The default is '\n' which is a useful default for Linux and OS X systems as well as web applications.

func OptNoAutoescape

func OptNoAutoescape() Option

OptNoAutoescape disables the XML/HTML autoescaping feature. It is disabled by default.

func OptNoKeepTrailingNewline

func OptNoKeepTrailingNewline() Option

OptNoKeepTrailingNewline disables the KeepTrailingNewline feature.

func OptNoLstripBlocks

func OptNoLstripBlocks() Option

OptNoLstripBlocks disables the LstripBlocks feature.

func OptNoTrimBlocks

func OptNoTrimBlocks() Option

OptNoTrimBlocks disables the TrimBlocks feature. It is disabled by default.

func OptRegisterCustomType

func OptRegisterCustomType(typ reflect.Type, getter exec.ValueFunc) Option

OptRegisterCustomType registers a custom value type that is not supported by default, e.g. an ordered map. If the value is nil, the type will be unregistered from the environment.

func OptSetExtensionConfig

func OptSetExtensionConfig(name string, config ext.Inheritable) Option

OptSetExtensionConfig sets a configuration for an extension. If the given config is nil, the named configuration will be removed from the environment.

func OptSetGlobal

func OptSetGlobal(name string, value any) Option

OptSetGlobal sets a global variable in the environment. If the value is nil, the variable will be removed from the environment.

func OptTrimBlocks

func OptTrimBlocks() Option

OptTrimBlocks enables the removal of the first newline after a block (block, not variable tag!). Disabled by default.

func OptUndefined

func OptUndefined(undefined exec.UndefinedFunc) Option

OptUndefined sets the behavior for undefined variables.

func OptVariableEndString

func OptVariableEndString(s string) Option

OptVariableEndString marks the end of a print statement. Defaults to '}}'.

func OptVariableStartString

func OptVariableStartString(s string) Option

OptVariableStartString marks the the beginning of a print statement. Defaults to '{{'.

Directories

Path Synopsis
ext
ansible Module
django Module
time Module

Jump to

Keyboard shortcuts

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