| 1 |
--- |
| 2 |
title: define |
| 3 |
description: Defines a template. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: |
| 10 |
signatures: [define NAME] |
| 11 |
--- |
| 12 |
|
| 13 |
Use with the [`block`][] statement: |
| 14 |
|
| 15 |
```go-html-template |
| 16 |
{{ block "main" . }} |
| 17 |
{{ print "default value if 'main' template is empty" }} |
| 18 |
{{ end }} |
| 19 |
|
| 20 |
{{ define "main" }} |
| 21 |
<h1>{{ .Title }}</h1> |
| 22 |
{{ .Content }} |
| 23 |
{{ end }} |
| 24 |
``` |
| 25 |
|
| 26 |
Use with the [`partial`][] function: |
| 27 |
|
| 28 |
```go-html-template |
| 29 |
{{ partial "inline/foo.html" (dict "answer" 42) }} |
| 30 |
|
| 31 |
{{ define "_partials/inline/foo.html" }} |
| 32 |
{{ printf "The answer is %v." .answer }} |
| 33 |
{{ end }} |
| 34 |
``` |
| 35 |
|
| 36 |
Use with the [`template`][] function: |
| 37 |
|
| 38 |
```go-html-template |
| 39 |
{{ template "foo" (dict "answer" 42) }} |
| 40 |
|
| 41 |
{{ define "foo" }} |
| 42 |
{{ printf "The answer is %v." .answer }} |
| 43 |
{{ end }} |
| 44 |
``` |
| 45 |
|
| 46 |
{{% include "/_common/functions/go-template/text-template.md" %}} |
| 47 |
|
| 48 |
[`block`]: /functions/go-template/block/ |
| 49 |
[`partial`]: /functions/partials/include/ |
| 50 |
[`template`]: /functions/go-template/block/ |