template functions

Functions for defining or executing templates.

tmpl.Exec

Execute (render) the named template. This is equivalent to using the template action, except the result is returned as a string.

This allows for post-processing of templates.

Usage

tmpl.Exec name [context]
context | tmpl.Exec name

Arguments

name description
name (required) The template’s name.
context (optional) The context to use.

Examples

$ gomplate -i '{{define "T1"}}hello, world!{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}'
HELLO, WORLD!
$ gomplate -i '{{define "T1"}}hello, {{.}}{{end}}{{ tmpl.Exec "T1" "world!" | strings.Title }}'
Hello, World!

tmpl.Inline

Alias: tpl

Render the given string as a template, just like a nested template.

If the template is given a name (see name argument below), it can be re-used later with the template keyword.

A context can be provided, otherwise the default gomplate context will be used.

Usage

tmpl.Inline [name] in [context]

Arguments

name description
name (optional) The template’s name.
in (required) The template to render, as a string
context (optional) The context to use when rendering - this becomes . inside the template.

Examples

$ gomplate -i '{{ tmpl.Inline "{{print `hello world`}}" }}'
hello world
$ gomplate -i '
{{ $tstring := "{{ print .value ` world` }}" }}
{{ $context := dict "value" "hello" }}
{{ tpl "T1" $tstring $context }}
{{ template "T1" (dict "value" "goodbye") }}
'
hello world
goodbye world