template
Executes the given template, optionally passing context.
Syntax
template NAME [CONTEXT]
Use the template
function to execute any of these embedded templates:
For example:
{{ range (.Paginate .Pages).Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ template "_internal/pagination.html" . }}
You can also use the template
function to execute a defined template:
{{ template "foo" (dict "answer" 42) }}
{{ define "foo" }}
{{ printf "The answer is %v." .answer }}
{{ end }}
The example above can be rewritten using an inline partial template:
{{ partial "inline/foo.html" (dict "answer" 42) }}
{{ define "partials/inline/foo.html" }}
{{ printf "The answer is %v." .answer }}
{{ end }}
The key distinctions between the preceding two examples are:
- Inline partials are globally scoped. That means that an inline partial defined in one template may be called from any template.
- Leveraging the
partialCached
function when calling an inline partial allows for performance optimization through result caching. - An inline partial can
return
a value of any data type instead of rendering a string.
See Go’s text/template documentation for more information.
Last updated:
April 13, 2025
:
content: Clarify usage of template function (719329530)
Improve this page