模板查找顺序
查找规则
Hugo 在为给定页面选择模板时会考虑以下列出的参数。模板按特异性排序。这应该会感觉很自然,但请查看下表,了解不同参数变化的具体示例。
- Kind
- 页面
Kind
(主页就是其中之一)。请参阅下面每个 Kind 的示例表。这还决定了它是一个**单页**(即常规内容页面。然后,我们会在_default/single.html
中查找 HTML 的模板)还是一个**列表页面**(章节列表、主页、分类列表、分类术语。然后,我们会在_default/list.html
中查找 HTML 的模板)。 - 布局
- 可以在前言中设置。
- 输出格式
- 请参阅 自定义输出格式。输出格式同时具有
name
(例如rss
、amp
、html
)和suffix
(例如xml
、html
)。我们更喜欢两者都匹配的(例如index.amp.html
),但会查找不太具体的模板。
请注意,如果输出格式的媒体类型定义了多个后缀,则仅考虑第一个。
- 语言
- 我们将在模板名称中考虑语言标签。如果站点语言是
fr
,则index.fr.amp.html
将胜过index.amp.html
,但index.amp.html
将在index.fr.html
之前被选中。 - 类型
- 如果在前言中设置,则为
type
的值,否则为根章节的名称(例如“blog”)。它将始终具有一个值,因此如果未设置,则该值为“page”。 - 章节
- 与
section
、taxonomy
和term
类型相关。
定位模板
您无法更改查找顺序来定位内容页面,但您可以更改内容页面来定位模板。在前言中指定 type
、layout
或两者。
考虑以下内容结构
content/
├── about.md
└── contact.md
content
目录根目录中的文件具有 page
的内容类型。要使用唯一的模板渲染这些页面,请创建一个匹配的子目录
layouts/
└── page/
└── single.html
但是联系页面可能有一个表单,需要一个不同的模板。在前言中指定 layout
content/contact.md。
layout: contact
title: Contact
layout = 'contact'
title = 'Contact'
{
"layout": "contact",
"title": "Contact"
}
然后创建联系页面的模板
layouts/
└── page/
└── contact.html <-- renders contact.md
└── single.html <-- renders about.md
作为内容类型,单词 page
很模糊。也许 miscellaneous
会更好。将 type
添加到每个页面的前言中
content/about.md。
title: About
type: miscellaneous
title = 'About'
type = 'miscellaneous'
{
"title": "About",
"type": "miscellaneous"
}
content/contact.md。
layout: contact
title: Contact
type: miscellaneous
layout = 'contact'
title = 'Contact'
type = 'miscellaneous'
{
"layout": "contact",
"title": "Contact",
"type": "miscellaneous"
}
现在将布局放置在相应的目录中
layouts/
└── miscellaneous/
└── contact.html <-- renders contact.md
└── single.html <-- renders about.md
主页模板
这些模板路径按特异性降序排序。最不具体的路径在每个列表的底部。
示例 | OutputFormat | 后缀 | 模板查找顺序 |
---|---|---|---|
主页 | html | html |
|
主页的基础模板 | html | html |
|
类型设置为“demotype”的主页 | html | html |
|
类型设置为“demotype”的主页的基础模板 | html | html |
|
布局设置为“demolayout”的首页 | html | html |
|
AMP 首页,法语 | amp | html |
|
JSON 首页 | json | json |
|
RSS 首页 | rss | xml |
|
单页模板
这些模板路径按特异性降序排序。最不具体的路径在每个列表的底部。
示例 | OutputFormat | 后缀 | 模板查找顺序 |
---|---|---|---|
“posts” 部分中的单页 | html | html |
|
“posts” 部分中单页的基础模板 | html | html |
|
“posts” 部分中布局设置为“demolayout”的单页 | html | html |
|
“posts” 部分中布局设置为“demolayout”的单页的基础模板 | html | html |
|
“posts” 部分中的 AMP 单页 | amp | html |
|
“posts” 部分中的 AMP 单页,法语 | amp | html |
|
章节模板
这些模板路径按特异性降序排序。最不具体的路径在每个列表的底部。
示例 | OutputFormat | 后缀 | 模板查找顺序 |
---|---|---|---|
“posts” 的章节列表 | html | html |
|
类型设置为“blog”的“posts”的章节列表 | html | html |
|
布局设置为“demolayout”的“posts”的章节列表 | html | html |
|
“posts” 的章节列表 | rss | xml |
|
分类模板
这些模板路径按特异性降序排序。最不具体的路径在每个列表的底部。
以下示例假设有以下站点配置
hugo。
taxonomies:
category: categories
[taxonomies]
category = 'categories'
{
"taxonomies": {
"category": "categories"
}
}
示例 | OutputFormat | 后缀 | 模板查找顺序 |
---|---|---|---|
“categories” 的分类列表 | html | html |
|
“categories” 的分类列表 | rss | xml |
|
术语模板
这些模板路径按特异性降序排序。最不具体的路径在每个列表的底部。
以下示例假设有以下站点配置
hugo。
taxonomies:
category: categories
[taxonomies]
category = 'categories'
{
"taxonomies": {
"category": "categories"
}
}
示例 | OutputFormat | 后缀 | 模板查找顺序 |
---|---|---|---|
“categories” 的术语列表 | html | html |
|
“categories” 的术语列表 | rss | xml |
|
RSS 模板
这些模板路径按特异性降序排序。最不具体的路径在每个列表的底部。
以下示例假设有以下站点配置
hugo。
taxonomies:
category: categories
[taxonomies]
category = 'categories'
{
"taxonomies": {
"category": "categories"
}
}
示例 | OutputFormat | 后缀 | 模板查找顺序 |
---|---|---|---|
RSS 首页 | rss | xml |
|
“posts” 的章节列表 | rss | xml |
|
“categories” 的分类列表 | rss | xml |
|
“categories” 的术语列表 | rss | xml |
|