模板查找顺序
查询规则
Hugo 在为给定页面选择模板时,会考虑以下参数。模板按其特异性排序。这应该感觉很自然,但请查看下表以了解不同参数变化的具体示例。
- 类型
- 页面的
类型
(首页就是一个)。请参见下面每个类型的示例表。这也决定了它是一个 单页 (即常规内容页。然后我们在_default/single.html
中查找 HTML 模板)还是一个 列表页 (章节列表、首页、分类列表、分类术语。然后我们在_default/list.html
中查找 HTML 模板)。 - 布局
- 可以在前置内容中设置。
- 输出格式
- 请参见 自定义输出格式 。输出格式既有
名称
(例如rss
、amp
、html
),也有后缀
(例如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 <-- 渲染 contact.md
└── single.html <-- 渲染 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 <-- 渲染 contact.md
└── single.html <-- 渲染 about.md
首页模板
这些模板路径按特异性降序排列。最不具体的路径位于每个列表的底部。
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
Home page | html | html |
|
Base template for home page | html | html |
|
Home page with type set to "demotype" | html | html |
|
Base template for home page with type set to "demotype" | html | html |
|
Home page with layout set to "demolayout" | html | html |
|
AMP home, French language | amp | html |
|
JSON home | json | json |
|
RSS home | rss | xml |
|
单页模板
这些模板路径按特异性降序排列。最不具体的路径位于每个列表的底部。
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
Single page in "posts" section | html | html |
|
Base template for single page in "posts" section | html | html |
|
Single page in "posts" section with layout set to "demolayout" | html | html |
|
Base template for single page in "posts" section with layout set to "demolayout" | html | html |
|
AMP single page in "posts" section | amp | html |
|
AMP single page in "posts" section, French language | amp | html |
|
章节模板
这些模板路径按特异性降序排列。最不具体的路径位于每个列表的底部。
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
Section list for "posts" | html | html |
|
Section list for "posts" with type set to "blog" | html | html |
|
Section list for "posts" with layout set to "demolayout" | html | html |
|
Section list for "posts" | rss | xml |
|
分类模板
这些模板路径按特异性降序排列。最不具体的路径位于每个列表的底部。
以下示例假设以下站点配置:
hugo.
taxonomies:
category: categories
[taxonomies]
category = 'categories'
{
"taxonomies": {
"category": "categories"
}
}
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
Taxonomy list for "categories" | html | html |
|
Taxonomy list for "categories" | rss | xml |
|
分类术语模板
这些模板路径按特异性降序排列。最不具体的路径位于每个列表的底部。
以下示例假设以下站点配置:
hugo.
taxonomies:
category: categories
[taxonomies]
category = 'categories'
{
"taxonomies": {
"category": "categories"
}
}
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
Term list for "categories" | html | html |
|
Term list for "categories" | rss | xml |
|
RSS 模板
这些模板路径按特异性降序排列。最不具体的路径位于每个列表的底部。
以下示例假设以下站点配置:
hugo.
taxonomies:
category: categories
[taxonomies]
category = 'categories'
{
"taxonomies": {
"category": "categories"
}
}
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
RSS home | rss | xml |
|
Section list for "posts" | rss | xml |
|
Taxonomy list for "categories" | rss | xml |
|
Term list for "categories" | rss | xml |
|