Debian Codegraph @

docs/content/en/templates/new-templatesystem-overview.md

raw
1 ---
2 title: New template system in Hugo v0.146.0
3 linktitle: New template system
4 description: Overview of the new template system in Hugo v0.146.0.
5 categories: []
6 keywords: []
7 weight: 1
8 ---
9
10 In [Hugo v0.146.0][], we performed a full re-implementation of how Go templates are handled in Hugo. This includes structural changes to the `layouts` folder and a new, more powerful template lookup system.
11
12 We have aimed to maintain as much backward compatibility as possible by mapping "old to new," but some reported breakages have occurred. We're working on a full overhaul of the documentation on this topic – until then, this is a one-pager with the most important changes.
13
14 ## Changes to the `layouts` folder
15
16 | Description | Action required |
17 |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
18 | The `_default` folder is removed. | Move all files in `layouts/_default` up to the `layouts/` root. |
19 | The `layouts/partials` folder is renamed to `layouts/_partials`. | Rename the folder. |
20 | The `layouts/shortcodes` folder is renamed to `layouts/_shortcodes`. | Rename the folder. |
21 | Any folder in `layouts` that does not start with `_` represents the root of a [Page path][]. In [Hugo v0.146.0][], this can be nested as deeply as needed, and `_shortcodes` and `_markup` folders can be placed at any level in the tree. | No action required. |
22 | The above also means that there's no top-level `layouts/taxonomy` or `layouts/section` folders anymore, unless it represents a [Page path][]. | Move them up to `layouts/` with one of the [Page kinds][] `section`, `taxonomy` or `term` as the base name, or place the layouts into the taxonomy [Page path][]. |
23 | A template named `taxonomy.html` used to be a candidate for both Page kind `term` and `taxonomy`, now it's only considered for `taxonomy`. | Create both `taxonomy.html` and `term.html` or create a more general layout, e.g. `list.html`. |
24 | For _base_ templates (e.g., `baseof.html`), in previous Hugo versions, you could prepend one identifier (layout, type, or kind) with a hyphen in front of the baseof keyword. | Move that identifier after the first "dot," e.g., rename`list-baseof.html` to `baseof.list.html`. |
25 | We have added a new `all` "catch-all" layout. This means that if you have, e.g., `layouts/all.html` and that is the only template, that layout will be used for all HTML page rendering. | |
26 | We have removed the concept of `_internal` Hugo templates.[^internal] | Replace constructs similar to `{{ template "_internal/opengraph.html" . }}` with `{{ partial "opengraph.html" . }}`. |
27 | The identifiers that can be used in a template filename are one of the [Page kinds][] (`home`, `page`, `section`, `taxonomy`, or `term`), one of the standard layouts (`list`, `single`, or `all`), a custom layout (as defined in the `layout` front matter field), a language (e.g., `en`), an output format (e.g., `html`, `rss`), and a suffix representing the media type. E.g., `all.en.html` and `home.rss.xml`. | |
28 | The above means that there's no such thing as an `index.html` template for the home page anymore. | Rename `index.html` to `home.html`. |
29
30 Also, see the [Example folder structure](#example-folder-structure) below for a more concrete example of the new layout system.
31
32 ## Changes to template lookup order
33
34 We have consolidated the template lookup so it works the same across all [template types][], with the exception of _partial_ templates which are not context-aware. The previous setup was difficult to understand and had a massive number of variants. The new setup aims to feel natural with few surprises.
35
36 The identifiers used in the template weighting, in order of importance, are:
37
38 | Identifier | Description |
39 |--------------------|-------------------------------------------------------|
40 | Layout custom | The custom `layout` set in front matter. |
41 | [Page kinds][] | One of `home`, `section`, `taxonomy`, `term`, `page`. |
42 | Layouts standard 1 | `list` or `single`. |
43 | Output format | The output format (e.g., `html`, `rss`). |
44 | Layouts standard 2 | `all`. |
45 | Language | The language (e.g., `en`). |
46 | Media type | The media type (e.g., `text/html`). |
47 | [Page path][] | The page path (e.g., `/blog/mypost`). |
48 | Type | `type` set in front matter.[^type] |
49
50 For templates placed in a `layouts` folder partly or completely matching a [Page path][], a closer match upwards will be considered _better_. In the [Example folder structure](#example-folder-structure) below, this means that:
51
52 - `layouts/docs/api/_markup/render-link.html` will be used to render links from the Page path `/docs/api` and below.
53 - `layouts/docs/baseof.html` will be used as the _base_ template for the Page path `/docs` and below.
54 - `layouts/tags/term.html` will be used for all `term` rendering in the `tags` taxonomy, except for the `blue` term, which will use `layouts/tags/blue/list.html`.
55
56 ## Example folder structure
57
58 ```tree
59 layouts
60 ├── baseof.html
61 ├── baseof.term.html
62 ├── home.html
63 ├── page.html
64 ├── section.html
65 ├── taxonomy.html
66 ├── term.html
67 ├── term.mylayout.en.rss.xml
68 ├── _markup
69 │ ├── render-codeblock-go.term.mylayout.no.rss.xml
70 │ └── render-link.html
71 ├── _partials
72 │ └── mypartial.html
73 ├── _shortcodes
74 │ ├── myshortcode.html
75 │ └── myshortcode.section.mylayout.en.rss.xml
76 ├── docs
77 │ ├── baseof.html
78 │ ├── _shortcodes
79 │ │ └── myshortcode.html
80 │ └── api
81 │ ├── mylayout.html
82 │ ├── page.html
83 │ └── _markup
84 │ └── render-link.html
85 └── tags
86 ├── taxonomy.html
87 ├── term.html
88 └── blue
89 └── list.html
90 ```
91
92 [^internal]: The old way of doing it made it difficult or impossible to, e.g., override `_internal/disqus.html` in a theme. Now you can just create a _partial_ template with the same name.
93 [^type]: The `type` set in front matter will effectively replace the `section` folder in [Page path][] when doing lookups.
94
95 [Hugo v0.146.0]: https://github.com/gohugoio/hugo/releases/tag/v0.146.0
96 [Page kinds]: https://gohugo.io/methods/page/kind/
97 [Page path]: https://gohugo.io/methods/page/path/
98 [template types]: /templates/types/