Taxonomies
概念上,Site
物件上的 Taxonomies
方法會回傳一個資料結構,像是:
taxonomy a:
- term 1:
- page 1
- page 2
- term 2:
- page 1
taxonomy b:
- term 1:
- page 2
- term 2:
- page 1
- page 2
[['taxonomy a']]
'term 1' = ['page 1', 'page 2']
[['taxonomy a']]
'term 2' = ['page 1']
[['taxonomy b']]
'term 1' = ['page 2']
[['taxonomy b']]
'term 2' = ['page 1', 'page 2']
{
"taxonomy a": [
{
"term 1": [
"page 1",
"page 2"
]
},
{
"term 2": [
"page 1"
]
}
],
"taxonomy b": [
{
"term 1": [
"page 2"
]
},
{
"term 2": [
"page 1",
"page 2"
]
}
]
}
例如,在一個書評網站中,你可能會創建兩個分類:一個是「類型」,另一個是「作者」。
使用此網站配置:
hugo.
taxonomies:
author: authors
genre: genres
[taxonomies]
author = 'authors'
genre = 'genres'
{
"taxonomies": {
"author": "authors",
"genre": "genres"
}
}
和此內容結構:
content/
├── books/
│ ├── and-then-there-were-none.md --> genres: suspense
│ ├── death-on-the-nile.md --> genres: suspense
│ └── jamaica-inn.md --> genres: suspense, romance
│ └── pride-and-prejudice.md --> genres: romance
└── _index.md
概念上,分類資料結構會像這樣:
authors:
- achristie:
- And Then There Were None
- Death on the Nile
- ddmaurier:
- Jamaica Inn
- jausten:
- Pride and Prejudice
genres:
- suspense:
- And Then There Were None
- Death on the Nile
- Jamaica Inn
- romance:
- Jamaica Inn
- Pride and Prejudice
[[authors]]
achristie = ['And Then There Were None', 'Death on the Nile']
[[authors]]
ddmaurier = ['Jamaica Inn']
[[authors]]
jausten = ['Pride and Prejudice']
[[genres]]
suspense = ['And Then There Were None', 'Death on the Nile', 'Jamaica Inn']
[[genres]]
romance = ['Jamaica Inn', 'Pride and Prejudice']
{
"authors": [
{
"achristie": [
"And Then There Were None",
"Death on the Nile"
]
},
{
"ddmaurier": [
"Jamaica Inn"
]
},
{
"jausten": [
"Pride and Prejudice"
]
}
],
"genres": [
{
"suspense": [
"And Then There Were None",
"Death on the Nile",
"Jamaica Inn"
]
},
{
"romance": [
"Jamaica Inn",
"Pride and Prejudice"
]
}
]
}
要列出「suspense」類型的書籍:
<ul>
{{ range .Site.Taxonomies.genres.suspense }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
Hugo 渲染後會顯示:
<ul>
<li><a href="/books/and-then-there-were-none/">And Then There Were None</a></li>
<li><a href="/books/death-on-the-nile/">Death on the Nile</a></li>
<li><a href="/books/jamaica-inn/">Jamaica Inn</a></li>
</ul>
範例
列出具有相同分類術語的內容
如果你將分類用於某些像是文章系列的內容,你可以列出與同一術語相關的頁面。例如:
<ul>
{{ range .Site.Taxonomies.series.golang }}
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
{{ end }}
</ul>
列出所有在指定分類中的內容
這在側邊欄顯示「精選內容」時非常有用。你甚至可以根據不同的術語為「精選內容」創建不同的區塊。
<section id="menu">
<ul>
{{ range $term, $taxonomy := .Site.Taxonomies.featured }}
<li>{{ $term }}</li>
<ul>
{{ range $taxonomy.Pages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}
</ul>
</section>
渲染網站的分類
以下範例顯示網站標籤分類中的所有術語:
<ul>
{{ range .Site.Taxonomies.tags }}
<li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
{{ end }}
</ul>
此範例會列出所有分類及其術語,並顯示分配給每個術語的所有內容。
layouts/partials/all-taxonomies.html
{{ with .Site.Taxonomies }}
{{ $numberOfTerms := 0 }}
{{ range $taxonomy, $terms := . }}
{{ $numberOfTerms = len . | add $numberOfTerms }}
{{ end }}
{{ if gt $numberOfTerms 0 }}
<ul>
{{ range $taxonomy, $terms := . }}
{{ with $terms }}
<li>
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
<ul>
{{ range $term, $weightedPages := . }}
<li>
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
<ul>
{{ range $weightedPages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
</li>
{{ end }}
</ul>
</li>
{{ end }}
{{ end }}
</ul>
{{ end }}
{{ end }}