Template lookup order
查找規則
Hugo 在選擇特定頁面的模板時會考慮以下參數。模板按具體程度排序。這應該是自然的,但請查看下面的表格,了解不同參數變化的具體例子。
- 類型
- 頁面的
Kind(首頁是其中之一)。請參閱每種類型的示例表格。這也決定了它是單一頁面(即常規內容頁面,我們會查找_default/single.html來渲染 HTML)還是列表頁面(區段列表、首頁、分類列表、分類術語。我們會查找_default/list.html來渲染 HTML)。 - 布局
- 可以在 Front Matter 中設定。
- 輸出格式
- 請參閱自訂輸出格式。輸出格式有一個
name(例如rss、amp、html)和一個suffix(例如xml、html)。我們優先匹配兩者(例如index.amp.html),但也會尋找較不具體的模板。
注意,如果輸出格式的媒體類型定義了多個後綴,僅會考慮第一個。
- 語言
- 我們會考慮模板名稱中的語言標籤。如果網站語言是
fr,那麼index.fr.amp.html會優先於index.amp.html,但index.amp.html會在index.fr.html之前被選擇。 - 類型
- 如果在 Front Matter 中設定了
type,則是其值;否則是根區段的名稱(例如 “blog”)。它總是有一個值,所以如果沒有設定,則默認值為 “page”。 - 區段
- 與
section、taxonomy和term類型有關。
針對模板
你無法更改查找順序來針對內容頁面,但可以更改內容頁面來指定目標模板。在 Front Matter 中指定 type、layout 或兩者。
考慮以下的內容結構:
content/
├── about.md
└── contact.md
位於內容目錄根目錄的檔案有一個內容類型,默認為 page。要使用獨特的模板渲染這些頁面,請創建對應的子目錄:
layouts/
└── page/
└── single.html
但是聯絡頁面可能有表單並需要不同的模板。在 Front Matter 中指定 layout:
content/contact.md.
layout: contact
title: 聯絡
layout = 'contact'
title = '聯絡'
{
"layout": "contact",
"title": "聯絡"
}
然後為聯絡頁面創建模板:
layouts/
└── page/
└── contact.html <-- 渲染 contact.md
└── single.html <-- 渲染 about.md
作為內容類型,page 這個詞比較模糊。也許 miscellaneous 會更好。將 type 添加到每個頁面的 Front Matter:
content/about.md.
title: 關於
type: miscellaneous
title = '關於'
type = 'miscellaneous'
{
"title": "關於",
"type": "miscellaneous"
}
content/contact.md.
layout: contact
title: 聯絡
type: miscellaneous
layout = 'contact'
title = '聯絡'
type = 'miscellaneous'
{
"layout": "contact",
"title": "聯絡",
"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 |
|