Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // NoTemplateFound indicates that a desired template cannot be located. NoTemplateFound = errors.New("no template found") // NoAssetFound indicates that no asset could be loaded. NoAssetFound = errors.New("no asset found") // IllegalName indicates that a name contains illegal characters or patterns. IllegalName = errors.New("name contains illegal patterns") )
var NamedTemplateSeparator = "#"
Functions ¶
This section is empty.
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func New ¶
New creates a new *Template and processes the given directories.
Each path should point to a "theme" directory. That directory is scanned for templates, which are compiled immediately and then cached.
Paths are normalized, but the resulting normalized version cannot have a relative path. So the path "foo/bar/.." is fine, and will evaluate to "foo", but the path "foo/../.." is not okay, as it will evaluate to "..", which represents a potential security risk.
Each path is scanned for files that end with the extension '.tpl'. Directories are not scanned recursively. Any other files or directories are ignored.
For convenience, the engine supports an additional set of template functions as defined in Sprig:
https://github.com/Masterminds/sprig
These can be disabled by not passing the Sprig functions into NewEngine.
func NewEngine ¶
NewEngine constructs a new *Engine. NewEngine provides more control over the template engine than New.
- funcMap is passed to the template. - options are passed to the template.
func (*Engine) Asset ¶
Asset returns the first matching asset path.
An asset is a non-template file or directory in a theme directory. This function returns the string path of the first path that matches.
An asset path is only returned if the asset exists and can be stat'ed.
func (*Engine) Dirs ¶
Dirs returns a list of directories that this Engine knows about.
Directories are presented in their cleaned, but not absolute, form.
func (*Engine) Render ¶
Render looks for a template with the given name, then executes it with the given data.
The 'name' parameter should be a relative template name (foo.tpl). This will look through all of the known templates and execute the first match found. Traversal order is the order in which the templates were added.
The 'data' will be passed into the template unaltered.
If the renderer cannot find a template, it returns NoTemplateFound. If the template cannot be rendered, it may return a different error.