|
1 | 1 | --- |
2 | 2 | title: Template lookup order |
3 | 3 | linkTitle: Lookup order |
4 | | -description: Hugo uses the rules below to select a template for a given page, starting from the most specific. |
| 4 | +description: Hugo selects templates by prioritizing the most specific match for each page, considering various factors to make that choice. |
5 | 5 | categories: [] |
6 | 6 | keywords: [] |
7 | 7 | weight: 20 |
8 | 8 | --- |
9 | 9 |
|
10 | | -{{< newtemplatesystem >}} |
| 10 | +> [!note] |
| 11 | +> Hugo [v0.146.0][] introduced a fully revised template system, with significant updates to: |
| 12 | +> |
| 13 | +> - File names |
| 14 | +> - Directory structure |
| 15 | +> - Lookup order |
| 16 | +> |
| 17 | +> When upgrading a site, theme, or module, please consult the [summary of changes][] for important migration details. |
| 18 | +
|
| 19 | +## File names |
| 20 | + |
| 21 | +The file name of each template in the `layouts` directory consists of two or more of the following identifiers separated by a dot: |
| 22 | + |
| 23 | +Identifier|Description|Example file name |
| 24 | +:--|:--|:-- |
| 25 | +base layout|Limited to [`baseof`][]|`baseof.html` |
| 26 | +custom layout|The value of the [`layout`][] front matter field, if any|`foo.html` |
| 27 | +page kind|One of [`home`][], [`page`][], [`section`][], [`taxonomy`][], or [`term`][]|`home.html` |
| 28 | +standard layout|Either [`list`][] or [`single`][]|`list.html` |
| 29 | +output format|The name of a configured [output format][]|`section.rss.xml` |
| 30 | +fallback layout|Limited to [`all`][]|`all.html` |
| 31 | +language|The key associated with a configured [language][]|`page.en.html` |
| 32 | +suffix|A configured suffix of the [media type][] associated with the output format |`section.rss.xml` |
| 33 | +designator|An arbitrary string, without dots, applicable to [partial], [shortcode], [content view][content view], and [render hook][render hook] templates.|`my-shortcode.html` |
| 34 | + |
| 35 | +Notes: |
| 36 | + |
| 37 | +1. The first identifier must be the base layout or designator, if applicable. The last identifier must be the suffix. The order of the other identifiers within the name is irrelevant. |
| 38 | + |
| 39 | + |
| 40 | +1. Always include a suffix. |
| 41 | +1. For base templates, the base layout identifier mucs |
| 42 | +1. Never include more than one of the page kind, standard layout, or fallback layout identifiers. For example, `home.html` is a valid file name, but `home.single.all.html` is not. |
| 43 | +1. In some cases the output format and suffix may have the same value. For example, the primary suffix of the `text/html` media type is `html`, and the `text/html` media type is associated with the `html` output format. In these cases you may provide one or both of the identifiers. For example, either `page.html.html` or `page.html` is correct. If both are provided, Hugo will choose the shortest file name. |
| 44 | +1. File names with invalid identifiers are not considered during template selection. For example, a template named `page.foo.bar.html` contains an invalid identifier, and will not be considered during template selection. While both `foo` and `bar` are valid custom layout identifiers, the file name may only contain _one_ custom layout identifier. |
| 45 | + |
| 46 | +Template file names follow this pattern, with a minimum of two identifiers, where the `designator` is only applicable to partial, shortcode, content view, and render hook templates: |
| 47 | + |
| 48 | +```text |
| 49 | +[designator].[page kind]|[standard layout]|[fallback layout].[custom layout].[language].[output format].[suffix] |
| 50 | +``` |
| 51 | + |
| 52 | +Examples: |
11 | 53 |
|
| 54 | +- `all.html.html` |
| 55 | +- `list.html.html` |
| 56 | +- `page.html.html` |
| 57 | +- `mylayout.html.html` |
| 58 | +- `mylayout.en.html.html` |
| 59 | +- `page.mylayout.html.html` |
| 60 | +- `page.mylayout.en.html.html` |
| 61 | +- `page.en.html.html` |
| 62 | + |
| 63 | +As noted earlier, when the output format and suffix have the same value, you may omit the output format identifier. For example, the list above is equivalent to: |
| 64 | + |
| 65 | +- `all.html` |
| 66 | +- `list.html` |
| 67 | +- `page.html` |
| 68 | +- `mylayout.html` |
| 69 | +- `mylayout.en.html` |
| 70 | +- `page.mylayout.html` |
| 71 | +- `page.mylayout.en.html` |
| 72 | +- `page.en.html` |
| 73 | + |
| 74 | +[`all`]: /templates/types/#all |
| 75 | +[`baseof`]: /templates/types/#base |
| 76 | +[`home`]: /templates/types/#home |
| 77 | +[`layout`]: /content-management/front-matter/#layout |
| 78 | +[`list`]: /templates/types/#list |
| 79 | +[`page`]: /templates/types/#page |
| 80 | +[`section`]: /templates/types/#section |
| 81 | +[`single`]: /templates/types/#single |
| 82 | +[`taxonomy`]: /templates/types/#taxonomy |
| 83 | +[`term`]: /templates/types/#term |
| 84 | +[content view]: /templates/types/#content-view |
| 85 | +[language]: /configuration/languages/ |
| 86 | +[media type]: /configuration/media-types/ |
| 87 | +[output format]: /configuration/output-formats/ |
| 88 | +[partial]: /templates/types/#partial |
| 89 | +[render hook]: /templates/types/#render-hook |
| 90 | +[shortcode]: /templates/types/#shortcode |
| 91 | +[summary of changes]: /templates/new-templatesystem-overview/ |
| 92 | +[v0.146.0]: https://github.com/gohugoio/hugo/releases/tag/v0.146.0 |
| 93 | + |
| 94 | +## Directory structure |
| 95 | + |
| 96 | + |
| 97 | +Symetricals |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +<!-- |
12 | 104 | ## Lookup rules |
13 | 105 |
|
14 | 106 | Hugo takes the parameters listed below into consideration when choosing a template for a given page. The templates are ordered by specificity. This should feel natural, but look at the table below for concrete examples of the different parameter variations. |
@@ -93,3 +185,5 @@ layouts/ |
93 | 185 | └── contact.html <-- renders contact.md |
94 | 186 | └── single.html <-- renders about.md |
95 | 187 | ``` |
| 188 | +
|
| 189 | +--> |
0 commit comments