mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-09 15:56:37 -04:00
Explain usage of front matter to target a template
This commit is contained in:
parent
5bd977257e
commit
6f34ca8e03
@ -39,6 +39,66 @@ Section
|
|||||||
Templates can live in either the project's or the themes' layout folders, 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.
|
Templates can live in either the project's or the themes' layout folders, 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.
|
||||||
{{% /note %}}
|
{{% /note %}}
|
||||||
|
|
||||||
|
## Target a template
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Consider this content structure:
|
||||||
|
|
||||||
|
```text
|
||||||
|
content/
|
||||||
|
├── about.md
|
||||||
|
└── contact.md
|
||||||
|
```
|
||||||
|
|
||||||
|
Files in the root of the content directory have a [content type] of `page`. To render these pages with a unique template, create a matching subdirectory:
|
||||||
|
|
||||||
|
[content type]: /getting-started/glossary/#content-type
|
||||||
|
|
||||||
|
```text
|
||||||
|
layouts/
|
||||||
|
└── page/
|
||||||
|
└── single.html
|
||||||
|
```
|
||||||
|
|
||||||
|
But the contact page probably has a form and requires a different template. In the front matter specify `layout`:
|
||||||
|
|
||||||
|
{{< code-toggle file=content/contact.md copy=false >}}
|
||||||
|
title = 'Contact'
|
||||||
|
layout = 'contact'
|
||||||
|
{{< /code-toggle >}}
|
||||||
|
|
||||||
|
Then create the template for the contact page:
|
||||||
|
|
||||||
|
```text
|
||||||
|
layouts/
|
||||||
|
└── page/
|
||||||
|
└── contact.html <-- renders contact.md
|
||||||
|
└── single.html <-- renders about.md
|
||||||
|
```
|
||||||
|
|
||||||
|
As a content type, the word `page` is vague. Perhaps `miscellaneous` would be better. Add `type` to the front matter of each page:
|
||||||
|
|
||||||
|
{{< code-toggle file=content/about.md copy=false >}}
|
||||||
|
title = 'About'
|
||||||
|
type = 'miscellaneous'
|
||||||
|
{{< /code-toggle >}}
|
||||||
|
|
||||||
|
{{< code-toggle file=content/contact.md copy=false >}}
|
||||||
|
title = 'Contact'
|
||||||
|
type = 'miscellaneous'
|
||||||
|
layout = 'contact'
|
||||||
|
{{< /code-toggle >}}
|
||||||
|
|
||||||
|
Now place the layouts in the corresponding directory:
|
||||||
|
|
||||||
|
```text
|
||||||
|
layouts/
|
||||||
|
└── miscellaneous/
|
||||||
|
└── contact.html <-- renders contact.md
|
||||||
|
└── single.html <-- renders about.md
|
||||||
|
```
|
||||||
|
|
||||||
## Home page
|
## Home page
|
||||||
|
|
||||||
{{< datatable-filtered "output" "layouts" "Kind == home" "Example" "OutputFormat" "Suffix" "Template Lookup Order" >}}
|
{{< datatable-filtered "output" "layouts" "Kind == home" "Example" "OutputFormat" "Suffix" "Template Lookup Order" >}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user