|
| 1 | + |
| 2 | +--- |
| 3 | +title: New template system in Hugo v0.146.0 |
| 4 | +description: Overview of the new template system in Hugo v0.146.0 |
| 5 | +categories: [] |
| 6 | +keywords: [] |
| 7 | +weight: 1 |
| 8 | +--- |
| 9 | + |
| 10 | + |
| 11 | +In [Hugo v0.146.0] we did a full re-implementation of how we handle Go templates in Hugo. This included some structural changes to the `layouts` folder and a new and more powerful template lookup system. |
| 12 | + |
| 13 | +We have strived to be as backwards compatible as possible. We're working on a full overhaul of the documentation about this topic. Until then, this is a one-pager with the most important changes. |
| 14 | + |
| 15 | +## Changes to the `layouts` folder |
| 16 | + |
| 17 | +| Description | Action required | |
| 18 | +| ------------- | ------------- | |
| 19 | +| The `_default` folder is removed. | Move all files in `layouts/_default` up to the `layouts` root.| |
| 20 | +| The `layouts/partials` folder is renamed to `layouts/_partials` | Rename the folder. | |
| 21 | +| The `layouts/shortcodes` folder is renamed to `layouts/_shortcodes` | Rename the folder. | |
| 22 | +| Any folder in `layouts` that not starts 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.| |
| 23 | +|The above also means that there's no top level `layouts/taxonomy` folder anymore.|Move them up to `layouts/` with one of the [Page kinds] `taxonomy` or `term` as the base name, or place the layouts into the taxonomy [Page path]. | |
| 24 | + For base templates (e.g. `baseof.html`) you could prepended one identifier (layout, type or kind) with a hyphen in front of the baseof keyword.|Move that identifier after the first "dot", e.g. `list-baseof.html` => `baseof.list.html`| |
| 25 | +|We have added a new `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 | +|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`.|| |
| 27 | +|The above means that there's no such thing as a `index.html` template for the home page anymore. | Rename `index.html` to `home.html`| |
| 28 | + |
| 29 | + |
| 30 | +## Changes template lookup order |
| 31 | + |
| 32 | +We have consolidated the template lookup so it should work the same across all of shortcodes, render hooks, partials, and page templates. What we had before was very hard to understand and had a massive amount of variants. What we have aimed for with the new setup is something that should feel natural with few surprises. |
| 33 | + |
| 34 | +The identifiers used in the template weighting, in approximate order of importance, are: |
| 35 | + |
| 36 | +| Identifier | Description | |
| 37 | +| ---------- | ----------- | |
| 38 | +| Custom layout | The custom `layout` set in front matter | |
| 39 | +| [Page kinds] | The page kind (`home`, `section`, `taxonomy`, `term`, `page`) | |
| 40 | +| Standard layouts 1 |`list` and `single`| |
| 41 | +| Output format | The output format (e.g. `html`, `rss`) | |
| 42 | +| Standard layouts 2 | `all` | |
| 43 | +| Language | The language (e.g. `en`) | |
| 44 | +| Media type | The media type (e.g. `text/html`)| |
| 45 | +| [Page path] | The page path (e.g. `/blog/mypost`) | |
| 46 | +| Type | If `type` is set in front matter, the value of `type` will replace the top level section in the Page path above.| |
| 47 | + |
| 48 | +For templates placed in the [Page path], a closer match will be considered _better_. In the example folder below, this means that: |
| 49 | + |
| 50 | +* `layouts/docs/api/_markup/render-link.html` will be used to render links from the Page path `/docs/api` and below. |
| 51 | +* `layouts/docs/baseof.html` will be used to render the base template for the Page path `/docs` and below. |
| 52 | + |
| 53 | +``` |
| 54 | +layouts |
| 55 | +├── baseof.html |
| 56 | +├── home.html |
| 57 | +├── list.html |
| 58 | +├── single.html |
| 59 | +├── taxonomy.html |
| 60 | +├── term.html |
| 61 | +├── term.mylayout.section.en.rss.xml |
| 62 | +├── _markup |
| 63 | +│ ├── render-codeblock-go.term.mylayout.no.rss.xml |
| 64 | +│ └── render-link.html |
| 65 | +├── _partials |
| 66 | +│ └── mypartial.html |
| 67 | +├── _shortcodes |
| 68 | +│ ├── myshortcode.html |
| 69 | +│ └── myshortcode.section.mylayout.en.rss.xml |
| 70 | +├── docs |
| 71 | +│ ├── baseof.html |
| 72 | +│ ├── _shortcodes |
| 73 | +│ │ └── myshortcode.html |
| 74 | +│ └── api |
| 75 | +│ ├── mylayout.html |
| 76 | +│ ├── single.html |
| 77 | +│ └── _markup |
| 78 | +│ └── render-link.html |
| 79 | +└── tags |
| 80 | + ├── taxonomy.html |
| 81 | + ├── term.html |
| 82 | + └── blue |
| 83 | + └── list.html |
| 84 | +
|
| 85 | +
|
| 86 | +[Hugo v0.146.0]: https://github.com/gohugoio/hugo/releases/tag/v0.146.0 |
| 87 | +[Page path]: https://gohugo.io/methods/page/path/ |
| 88 | +[Page kinds]: https://gohugo.io/methods/page/kind/ |
0 commit comments