| 1 |
--- |
| 2 |
title: template |
| 3 |
description: Executes the given template, optionally passing context. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: |
| 10 |
signatures: ['template NAME [CONTEXT]'] |
| 11 |
--- |
| 12 |
|
| 13 |
Use the `template` statement to execute a defined template: |
| 14 |
|
| 15 |
```go-html-template |
| 16 |
{{ template "foo" (dict "answer" 42) }} |
| 17 |
|
| 18 |
{{ define "foo" }} |
| 19 |
{{ printf "The answer is %v." .answer }} |
| 20 |
{{ end }} |
| 21 |
``` |
| 22 |
|
| 23 |
The example above can be rewritten using an inline _partial_ template: |
| 24 |
|
| 25 |
```go-html-template |
| 26 |
{{ partial "inline/foo.html" (dict "answer" 42) }} |
| 27 |
|
| 28 |
{{ define "_partials/inline/foo.html" }} |
| 29 |
{{ printf "The answer is %v." .answer }} |
| 30 |
{{ end }} |
| 31 |
``` |
| 32 |
|
| 33 |
The key distinctions between the preceding two examples are: |
| 34 |
|
| 35 |
1. Inline _partial_ templates are globally scoped. That means that an inline _partial_ template defined in one template may be called from any template. |
| 36 |
1. Leveraging the [`partialCached`][] function when calling an inline _partial_ template allows for performance optimization through result caching. |
| 37 |
1. An inline _partial_ template can [`return`][] a value of any data type instead of rendering a string. |
| 38 |
|
| 39 |
{{% include "/_common/functions/go-template/text-template.md" %}} |
| 40 |
|
| 41 |
[`partialCached`]: /functions/partials/includecached/ |
| 42 |
[`return`]: /functions/go-template/return/ |