Debian Codegraph @

docs/content/en/functions/go-template/block.md

raw
1 ---
2 title: block
3 description: Defines a template and executes it in place.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType:
10 signatures: [block NAME CONTEXT]
11 ---
12
13 A block is shorthand for defining a template:
14
15 ```go-html-template
16 {{ define "name" }} T1 {{ end }}
17 ```
18
19 and then executing it in place:
20
21 ```go-html-template
22 {{ template "name" pipeline }}
23 ```
24
25 The typical use is to define a set of root templates that are then customized by redefining the block templates within.
26
27 ```go-html-template {file="layouts/baseof.html"}
28 <body>
29 <main>
30 {{ block "main" . }}
31 {{ print "default value if 'main' template is empty" }}
32 {{ end }}
33 </main>
34 </body>
35 ```
36
37 ```go-html-template {file="layouts/page.html"}
38 {{ define "main" }}
39 <h1>{{ .Title }}</h1>
40 {{ .Content }}
41 {{ end }}
42 ```
43
44 ```go-html-template {file="layouts/section.html"}
45 {{ define "main" }}
46 <h1>{{ .Title }}</h1>
47 {{ .Content }}
48 {{ range .Pages }}
49 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
50 {{ end }}
51 {{ end }}
52 ```
53
54 {{% include "/_common/functions/go-template/text-template.md" %}}