Skip to content

Commit

Permalink
Explain usage of front matter to target a template
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring authored Oct 13, 2023
1 parent 5bd9772 commit 6f34ca8
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions content/en/templates/lookup-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{{% /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

{{< datatable-filtered "output" "layouts" "Kind == home" "Example" "OutputFormat" "Suffix" "Template Lookup Order" >}}
Expand Down

0 comments on commit 6f34ca8

Please sign in to comment.