Documentation ¶
Overview ¶
A jinja like template-engine for Go.
Index ¶
- Variables
- func FromFile(path string) (*exec.Template, error)
- func Must(tpl *exec.Template, err error) *exec.Template
- type Environment
- type Option
- func OptAutoescape() Option
- func OptBlockEndString(s string) Option
- func OptBlockStartString(s string) Option
- func OptCommentEndString(s string) Option
- func OptCommentStartString(s string) Option
- func OptKeepTrailingNewline() Option
- func OptLineCommentPrefix(s string) Option
- func OptLineStatementPrefix(s string) Option
- func OptLoader(loader loaders.Loader) Option
- func OptLstripBlocks() Option
- func OptNewlineSequence(s string) Option
- func OptNoAutoescape() Option
- func OptNoKeepTrailingNewline() Option
- func OptNoLstripBlocks() Option
- func OptNoTrimBlocks() Option
- func OptRegisterCustomType(typ reflect.Type, getter exec.ValueFunc) Option
- func OptSetExtensionConfig(name string, config ext.Inheritable) Option
- func OptSetGlobal(name string, value any) Option
- func OptTrimBlocks() Option
- func OptUndefined(undefined exec.UndefinedFunc) Option
- func OptVariableEndString(s string) Option
- func OptVariableStartString(s string) Option
Constants ¶
This section is empty.
Variables ¶
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 )
var ( Undefined exec.UndefinedFunc = exec.NewUndefinedValue StrictUndefined exec.UndefinedFunc = exec.NewStrictUndefinedValue ChainedUndefined exec.UndefinedFunc = exec.NewChainedUndefinedValue ChainedStrictUndefined exec.UndefinedFunc = exec.NewChainedStrictUndefinedValue )
convenient interface to create a new UndefinedFunc
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 ¶
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 ¶
OptBlockEndString marks the end of a block. Defaults to '%}'.
func OptBlockStartString ¶
OptBlockStartString marks the beginning of a block. Defaults to '{%'
func OptCommentEndString ¶
OptCommentEndString marks the end of a comment. Defaults to '#}'.
func OptCommentStartString ¶
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 ¶
OptLineCommentPrefix will be used as prefix for line based comments, if given and a string.
func OptLineStatementPrefix ¶
OptLineStatementPrefix will be used as prefix for line based statements, if given and a string.
func OptLoader ¶
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 ¶
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 ¶
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 ¶
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 ¶
OptVariableEndString marks the end of a print statement. Defaults to '}}'.
func OptVariableStartString ¶
OptVariableStartString marks the the beginning of a print statement. Defaults to '{{'.