| 1 |
--- |
| 2 |
title: Template lookup order |
| 3 |
linkTitle: Lookup order |
| 4 |
description: Hugo uses the rules below to select a template for a given page, starting from the most specific. |
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
weight: 20 |
| 8 |
--- |
| 9 |
|
| 10 |
{{< newtemplatesystem >}} |
| 11 |
|
| 12 |
## Lookup rules |
| 13 |
|
| 14 |
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. |
| 15 |
|
| 16 |
Kind |
| 17 |
: The page `Kind` (the home page is one). See the example tables below per kind. This also determines if it is a **single page** (i.e. a regular content page. We then look for a template in `_default/single.html` for HTML) or a **list page** (section listings, home page, taxonomy lists, taxonomy terms. We then look for a template in `_default/list.html` for HTML). |
| 18 |
|
| 19 |
Layout |
| 20 |
: Can be set in front matter. |
| 21 |
|
| 22 |
Output Format |
| 23 |
: See [configure output formats](/configuration/output-formats/). An output format has both a `name` (e.g. `rss`, `amp`, `html`) and a `suffix` (e.g. `xml`, `html`). We prefer matches with both (e.g. `index.amp.html`), but look for less specific templates. |
| 24 |
|
| 25 |
Note that if the output format's Media Type has more than one suffix defined, only the first is considered. |
| 26 |
|
| 27 |
Language |
| 28 |
: We will consider a language tag in the template name. If the site language is `fr`, `index.fr.amp.html` will win over `index.amp.html`, but `index.amp.html` will be chosen before `index.fr.html`. |
| 29 |
|
| 30 |
Type |
| 31 |
: Is value of `type` if set in front matter, else it is the name of the root section (e.g. "blog"). It will always have a value, so if not set, the value is "page". |
| 32 |
|
| 33 |
Section |
| 34 |
: Is relevant for `section`, `taxonomy` and `term` types. |
| 35 |
|
| 36 |
> [!NOTE] |
| 37 |
> Templates can live in either the project's or the themes' `layout` directories, and the most specific templates will be chosen. Hugo will interleave the lookups listed below, finding the most specific one either in the project or themes. |
| 38 |
|
| 39 |
## Target a template |
| 40 |
|
| 41 |
You cannot change the lookup order to target a content page, but you can change a content page to target a template. Specify `type`, `layout`, or both in front matter. |
| 42 |
|
| 43 |
Consider this content structure: |
| 44 |
|
| 45 |
```tree |
| 46 |
content/ |
| 47 |
├── about.md |
| 48 |
└── contact.md |
| 49 |
``` |
| 50 |
|
| 51 |
Files in the root of the `content` directory have a [content type](g) of `page`. To render these pages with a unique template, create a matching subdirectory: |
| 52 |
|
| 53 |
```tree |
| 54 |
layouts/ |
| 55 |
└── page/ |
| 56 |
└── single.html |
| 57 |
``` |
| 58 |
|
| 59 |
The contact page, however, probably has a form and requires a different template. In the front matter specify `layout`: |
| 60 |
|
| 61 |
{{< code-toggle file=content/contact.md fm=true >}} |
| 62 |
title = 'Contact' |
| 63 |
layout = 'contact' |
| 64 |
{{< /code-toggle >}} |
| 65 |
|
| 66 |
Then create the template for the contact page: |
| 67 |
|
| 68 |
```tree |
| 69 |
layouts/ |
| 70 |
└── page/ |
| 71 |
└── contact.html <-- renders contact.md |
| 72 |
└── single.html <-- renders about.md |
| 73 |
``` |
| 74 |
|
| 75 |
As a content type, the word `page` is vague. Perhaps `miscellaneous` would be better. Add `type` to the front matter of each page: |
| 76 |
|
| 77 |
{{< code-toggle file=content/about.md fm=true >}} |
| 78 |
title = 'About' |
| 79 |
type = 'miscellaneous' |
| 80 |
{{< /code-toggle >}} |
| 81 |
|
| 82 |
{{< code-toggle file=content/contact.md fm=true >}} |
| 83 |
title = 'Contact' |
| 84 |
type = 'miscellaneous' |
| 85 |
layout = 'contact' |
| 86 |
{{< /code-toggle >}} |
| 87 |
|
| 88 |
Now place the layouts in the corresponding directory: |
| 89 |
|
| 90 |
```tree |
| 91 |
layouts/ |
| 92 |
└── miscellaneous/ |
| 93 |
└── contact.html <-- renders contact.md |
| 94 |
└── single.html <-- renders about.md |
| 95 |
``` |