Term templates
下列的 項目 模板繼承了網站的基本模板,並顯示與當前項目相關的頁面列表。
layouts/_default/term.html
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ range .Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ end }}
請參閱 模板查找順序 以選擇提供所需具體層級的模板路徑。
在上述範例中,如果項目的對應頁面沒有由檔案支持,它將會以大寫字母開頭。你可以在網站配置中禁用此功能:
hugo.
capitalizeListTitles: false
capitalizeListTitles = false
{
"capitalizeListTitles": false
}
資料對象
在項目模板中使用以下方法來操作 Data
物件。
- 單數
- (
string
) 返回範疇的單數名稱。
{{ .Data.Singular }} → tag
- 複數
- (
string
) 返回範疇的複數名稱。
{{ .Data.Plural }} → tags
- 項目
- (
string
) 返回項目的名稱。
{{ .Data.Term }} → fiction
顯示元數據
透過在內容目錄中創建對應的分支捆綁,你可以顯示每個項目的元數據。
例如,創建一個 “authors” 範疇:
hugo.
taxonomies:
author: authors
[taxonomies]
author = 'authors'
{
"taxonomies": {
"author": "authors"
}
}
接著為每個項目創建一個 分支捆綁:
content/
└── authors/
├── jsmith/
│ ├── _index.md
│ └── portrait.jpg
└── rjones/
├── _index.md
└── portrait.jpg
然後在每個項目頁面中加入 front matter:
content/authors/jsmith/_index.md
---
affiliation: University of Chicago
title: John Smith
---
+++
affiliation = 'University of Chicago'
title = 'John Smith'
+++
{
"affiliation": "University of Chicago",
"title": "John Smith"
}
接著創建一個針對 “authors” 範疇的項目模板:
layouts/authors/term.html
{{ define "main" }}
<h1>{{ .Title }}</h1>
<p>隸屬機構: {{ .Params.affiliation }}</p>
{{ with .Resources.Get "portrait.jpg" }}
{{ with .Fill "100x100" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="portrait">
{{ end }}
{{ end }}
{{ .Content }}
{{ range .Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ end }}
在上述範例中,我們顯示了作者及其隸屬機構和肖像照,接著列出相關內容。