Debian Codegraph @

docs/content/en/templates/types.md

raw
1 ---
2 title: Template types
3 description: Create templates of different types to render your content, resources, and data.
4 categories: []
5 keywords: []
6 weight: 30
7 aliases: [
8 '/templates/base/',
9 '/templates/content-view/',
10 '/templates/home/',
11 '/templates/lists/',
12 '/templates/partial/',
13 '/templates/section/',
14 '/templates/single/',
15 '/templates/taxonomy/',
16 '/templates/term/',
17 ]
18 ---
19
20 ## Structure
21
22 Create templates in the `layouts` directory in the root of your project.
23
24 Although your site may not require each of these templates, the example below is typical for a site of medium complexity.
25
26 ```tree
27 layouts/
28 ├── _markup/
29 │ ├── render-image.html <-- render hook
30 │ └── render-link.html <-- render hook
31 ├── _partials/
32 │ ├── footer.html
33 │ └── header.html
34 ├── _shortcodes/
35 │ ├── audio.html
36 │ └── video.html
37 ├── books/
38 │ ├── page.html
39 │ └── section.html
40 ├── films/
41 │ ├── _views/
42 │ │ └── card.html <-- view template
43 │ ├── page.html
44 │ └── section.html
45 ├── baseof.html
46 ├── home.html
47 ├── page.html
48 ├── section.html
49 ├── taxonomy.html
50 └── term.html
51 ```
52
53 Hugo's [template lookup order][] determines the template path, allowing you to create unique templates for any page.
54
55 > [!NOTE]
56 > You must have thorough understanding of the template lookup order when creating templates. Template selection is based on template type, page kind, content type, section, language, and output format.
57
58 The purpose of each template type is described below.
59
60 ## Base
61
62 A _base_ template serves as a foundational layout that other templates can build upon. It typically defines the common structural components of your HTML, such as the `html`, `head`, and `body` elements. It also often includes recurring features like headers, footers, navigation, and script inclusions that appear across multiple pages of your site. By defining these common aspects once in a _base_ template, you avoid redundancy, ensure consistency, and simplify the maintenance of your website.
63
64 Hugo can apply a _base_ template to the following template types: [home](#home), [page](#page), [section](#section), [taxonomy](#taxonomy), [term](#term), [single](#single), [list](#list), and [all](#all). When Hugo parses any of these template types, it will apply a _base_ template only if the template being parsed meets these specific conditions:
65
66 - It must include at least one [`define`][] [action](g).
67 - It can only contain `define` actions, whitespace, and [template comments][]. No other content is allowed.
68
69 > [!NOTE]
70 > If a template doesn't meet all these criteria, Hugo executes it exactly as provided, without applying a _base_ template.
71
72 When Hugo applies a _base_ template, it replaces its [`block`][] actions with content from the corresponding `define` actions found in the template to which the _base_ template is applied.
73
74 For example, the _base_ template below calls the [`partial`][] function to include `head`, `header`, and `footer` elements. The `block` action acts as a placeholder, and its content will be replaced by a matching `define` action from the template to which it is applied.
75
76 ```go-html-template {file="layouts/baseof.html"}
77 <!DOCTYPE html>
78 <html lang="{{ site.Language.Locale }}" dir="{{ or site.Language.Direction `ltr` }}">
79 <head>
80 {{ partial "head.html" . }}
81 </head>
82 <body>
83 <header>
84 {{ partial "header.html" . }}
85 </header>
86 <main>
87 {{ block "main" . }}
88 This will be replaced with content from the
89 corresponding "define" action found in the template
90 to which this _base_ template is applied.
91 {{ end }}
92 </main>
93 <footer>
94 {{ partial "footer.html" . }}
95 </footer>
96 </body>
97 </html>
98 ```
99
100 ```go-html-template {file="layouts/home.html"}
101 {{ define "main" }}
102 This will replace the content of the "block" action
103 found in the _base_ template.
104 {{ end }}
105 ```
106
107 ## Home
108
109 A _home_ template renders your site's home page.
110
111 For example, Hugo applies a _base_ template to the _home_ template below, then renders the page content and a list of the site's regular pages.
112
113 ```go-html-template {file="layouts/home.html"}
114 {{ define "main" }}
115 {{ .Content }}
116 {{ range .Site.RegularPages }}
117 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
118 {{ end }}
119 {{ end }}
120 ```
121
122 {{% include "/_common/filter-sort-group.md" %}}
123
124 ## Page
125
126 A _page_ template renders a regular page.
127
128 For example, Hugo applies a _base_ template to the _page_ template below, then renders the page title and page content.
129
130 ```go-html-template {file="layouts/page.html"}
131 {{ define "main" }}
132 <h1>{{ .Title }}</h1>
133 {{ .Content }}
134 {{ end }}
135 ```
136
137 ## Section
138
139 A _section_ template renders a list of pages within a [section](g).
140
141 For example, Hugo applies a _base_ template to the _section_ template below, then renders the page title, page content, and a list of pages in the current section.
142
143 ```go-html-template {file="layouts/section.html"}
144 {{ define "main" }}
145 <h1>{{ .Title }}</h1>
146 {{ .Content }}
147 {{ range .Pages }}
148 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
149 {{ end }}
150 {{ end }}
151 ```
152
153 {{% include "/_common/filter-sort-group.md" %}}
154
155 ## Taxonomy
156
157 A _taxonomy_ template renders a list of terms in a [taxonomy](g).
158
159 For example, Hugo applies a _base_ template to the _taxonomy_ template below, then renders the page title, page content, and a list of [terms](g) in the current taxonomy.
160
161 ```go-html-template {file="layouts/taxonomy.html"}
162 {{ define "main" }}
163 <h1>{{ .Title }}</h1>
164 {{ .Content }}
165 {{ range .Pages }}
166 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
167 {{ end }}
168 {{ end }}
169 ```
170
171 {{% include "/_common/filter-sort-group.md" %}}
172
173 Within a _taxonomy_ template, the [`Data`][] object provides these taxonomy-specific methods:
174
175 - [`Singular`][taxonomy-singular]
176 - [`Plural`][taxonomy-plural]
177 - [`Terms`][]
178
179 The `Terms` method returns a [taxonomy object](g), allowing you to call any of its methods including [`Alphabetical`][] and [`ByCount`][]. For example, use the `ByCount` method to render a list of terms sorted by the number of pages associated with each term:
180
181 ```go-html-template {file="layouts/taxonomy.html"}
182 {{ define "main" }}
183 <h1>{{ .Title }}</h1>
184 {{ .Content }}
185 {{ range .Data.Terms.ByCount }}
186 <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a> ({{ .Count }})</h2>
187 {{ end }}
188 {{ end }}
189 ```
190
191 ## Term
192
193 A _term_ template renders a list of pages associated with a [term](g).
194
195 For example, Hugo applies a _base_ template to the _term_ template below, then renders the page title, page content, and a list of pages associated with the current term.
196
197 ```go-html-template {file="layouts/term.html"}
198 {{ define "main" }}
199 <h1>{{ .Title }}</h1>
200 {{ .Content }}
201 {{ range .Pages }}
202 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
203 {{ end }}
204 {{ end }}
205 ```
206
207 {{% include "/_common/filter-sort-group.md" %}}
208
209 Within a _term_ template, the [`Data`][] object provides these term-specific methods:
210
211 - [`Singular`][term-singular]
212 - [`Plural`][term-plural]
213 - [`Term`][]
214
215 ## Single
216
217 A _single_ template is a fallback for a _page_ template. If a _page_ template does not exist, Hugo will look for a _single_ template instead.
218
219 For example, Hugo applies a _base_ template to the _single_ template below, then renders the page title and page content.
220
221 ```go-html-template {file="layouts/single.html"}
222 {{ define "main" }}
223 <h1>{{ .Title }}</h1>
224 {{ .Content }}
225 {{ end }}
226 ```
227
228 ## List
229
230 A _list_ template is a fallback for [home](#home), [section](#section), [taxonomy](#taxonomy), and [term](#term) templates. If one of these template types does not exist, Hugo will look for a _list_ template instead.
231
232 For example, Hugo applies a _base_ template to the _list_ template below, then renders the page title, page content, and a list of pages.
233
234 ```go-html-template {file="layouts/list.html"}
235 {{ define "main" }}
236 <h1>{{ .Title }}</h1>
237 {{ .Content }}
238 {{ range .Pages }}
239 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
240 {{ end }}
241 {{ end }}
242 ```
243
244 ## All
245
246 An _all_ template is a fallback for [home](#home), [page](#page), [section](#section), [taxonomy](#taxonomy), [term](#term), [single](#single), and [list](#list) templates. If one of these template types does not exist, Hugo will look for an _all_ template instead.
247
248 For example, Hugo applies a _base_ template to the _all_ template below, then conditionally renders a page based on its page kind.
249
250 ```go-html-template {file="layouts/all.html"}
251 {{ define "main" }}
252 {{ if eq .Kind "home" }}
253 {{ .Content }}
254 {{ range .Site.RegularPages }}
255 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
256 {{ end }}
257 {{ else if eq .Kind "page" }}
258 <h1>{{ .Title }}</h1>
259 {{ .Content }}
260 {{ else if in (slice "section" "taxonomy" "term") .Kind }}
261 <h1>{{ .Title }}</h1>
262 {{ .Content }}
263 {{ range .Pages }}
264 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
265 {{ end }}
266 {{ else }}
267 {{ errorf "Unsupported page kind: %s" .Kind }}
268 {{ end }}
269 {{ end }}
270 ```
271
272 ## Partial
273
274 A _partial_ template is typically used to render a component of your site, though you may also create _partial_ templates that return values.
275
276 For example, the _partial_ template below renders copyright information:
277
278 ```go-html-template {file="layouts/_partials/footer.html"}
279 <p>Copyright {{ now.Year }}. All rights reserved.</p>
280 ```
281
282 Execute the _partial_ template by calling the [`partial`][] or [`partialCached`][] function, optionally passing context as the second argument:
283
284 ```go-html-template {file="layouts/baseof.html"}
285 {{ partial "footer.html" . }}
286 ```
287
288 <!-- https://github.com/gohugoio/hugo/pull/13614#issuecomment-2805977008 -->
289 Unlike other template types, Hugo does not consider the current page kind, content type, logical path, language, or output format when searching for a matching _partial_ template. However, it _does_ apply the same _name_ matching logic it uses for other template types. This means it tries to find the most specific match first, then progressively looks for more general versions if the specific one isn't found.
290
291 For example, with this call:
292
293 ```go-html-template {file="layouts/baseof.html"}
294 {{ partial "footer.section.de.html" . }}
295 ```
296
297 Hugo uses this lookup order to find a matching template:
298
299 1. `layouts/_partials/footer.section.de.html`
300 1. `layouts/_partials/footer.section.html`
301 1. `layouts/_partials/footer.de.html`
302 1. `layouts/_partials/footer.html`
303
304 A _partial_ template can also be defined inline within another template. However, it's important to note that the template namespace is global; ensuring unique names for these _partial_ templates is necessary to prevent conflicts.
305
306 ```go-html-template
307 Value: {{ partial "my-inline-partial.html" . }}
308
309 {{ define "_partials/my-inline-partial.html" }}
310 {{ $value := 32 }}
311 {{ return $value }}
312 {{ end }}
313 ```
314
315 ## View
316
317 A _view_ template is similar to a _partial_ template, invoked by calling the [`Render`][] method on a `Page` object. Unlike _partial_ templates, _view_ templates:
318
319 - Inherit the context of the current page
320 - Can target any page kind, content type, logical path, language, or output format
321 - Can reside at any level within the `layouts` directory
322
323 For example, Hugo applies a _base_ template to the _home_ template below, then renders the page content and a card component for each page within the `films` section of your site.
324
325 ```go-html-template {file="layouts/home.html"}
326 {{ define "main" }}
327 {{ .Content }}
328 <ul>
329 {{ range where site.RegularPages "Section" "films" }}
330 {{ .Render "_views/card" }}
331 {{ end }}
332 </ul>
333 {{ end }}
334 ```
335
336 ```go-html-template {file="layouts/films/_views/card.html"}
337 <div class="card">
338 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
339 {{ .Summary }}
340 </div>
341 ```
342
343 See the [`Render`][] method documentation for guidance on naming and organizing _view_ templates.
344
345 ## Render hook
346
347 A _render hook_ template overrides the conversion of Markdown to HTML.
348
349 For example, the _render hook_ template below adds an anchor link to the right of each heading.
350
351 ```go-html-template {file="layouts/_markup/render-heading.html"}
352 <h{{ .Level }} id="{{ .Anchor }}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
353 {{ .Text }}
354 <a href="#{{ .Anchor }}">#</a>
355 </h{{ .Level }}>
356 ```
357
358 Learn more about [render hook templates][].
359
360 ## Shortcode
361
362 A _shortcode_ template is used to render a component of your site. Unlike _partial_ or _view_ templates, _shortcode_ templates are called from content pages.
363
364 For example, the _shortcode_ template below renders an audio element from a [global resource](g).
365
366 ```go-html-template {file="layouts/_shortcodes/audio.html"}
367 {{ with resources.Get (.Get "src") }}
368 <audio controls preload="auto" src="{{ .RelPermalink }}"></audio>
369 {{ end }}
370 ```
371
372 Then call the shortcode from within markup:
373
374 ```md {file="content/example.md"}
375 {{</* audio src=/audio/test.mp3 */>}}
376 ```
377
378 Learn more about [shortcode templates][].
379
380 ## Other
381
382 Use other specialized templates to create:
383
384 - [Sitemaps][]
385 - [RSS feeds][]
386 - [404 error pages][]
387 - [robots.txt files][]
388
389 [404 error pages]: /templates/404/
390 [RSS feeds]: /templates/rss/
391 [Sitemaps]: /templates/sitemap/
392 [`Alphabetical`]: /methods/taxonomy/alphabetical/
393 [`ByCount`]: /methods/taxonomy/bycount/
394 [`Data`]: /methods/page/data/
395 [`Render`]: /methods/page/render/
396 [`Term`]: /methods/page/data/#term
397 [`Terms`]: /methods/page/data/#terms
398 [`block`]: /functions/go-template/block/
399 [`define`]: /functions/go-template/define/
400 [`partialCached`]: /functions/partials/includeCached/
401 [`partial`]: /functions/partials/include/
402 [render hook templates]: /render-hooks/
403 [robots.txt files]: /templates/robots/
404 [shortcode templates]: /templates/shortcode/
405 [taxonomy-plural]: /methods/page/data/#plural
406 [taxonomy-singular]: /methods/page/data/#singular
407 [template comments]: /templates/introduction/#comments
408 [template lookup order]: /templates/lookup-order/
409 [term-plural]: /methods/page/data/#plural-1
410 [term-singular]: /methods/page/data/#singular-1