HUGO
Menu
GitHub 87548 stars Mastodon

Hugo 分類法

Hugo 包含對用戶定義分類法的支持。

什麼是分類法?

Hugo 包含對稱為 taxonomies(分類法)的用戶定義內容分組的支持。分類法是內容之間邏輯關系的分類。

定義

Taxonomy(分類法)
可用於分類內容的分類
Term(術語)
分類法中的鍵
Value(值)
分配給術語的內容片段

示例分類法:電影網站

假設你正在制作一個關於電影的網站。你可能希望包含以下分類法:

  • Actors(演員)
  • Directors(導演)
  • Studios(制片公司)
  • Genre(類型)
  • Year(年份)
  • Awards(獎項)

然後,在每部電影中,你將指定每個分類法的術語(即,在每部電影內容文件的 front matter 中)。根據這些術語,Hugo 將自動為每個演員、導演、制片公司、類型、年份和獎項創建頁面,每個頁面列出所有匹配該特定演員、導演、制片公司、類型、年份和獎項的電影。

電影分類法組織

繼續以電影網站為例,以下從分類法的角度演示了內容關系:

Actor                    <- 分類法
    Bruce Willis         <- 術語
        The Sixth Sense  <- 值
        Unbreakable      <- 值
        Moonrise Kingdom <- 值
    Samuel L. Jackson    <- 術語
        Unbreakable      <- 值
        The Avengers     <- 值
        xXx              <- 值

從內容的角度來看,關系將顯示不同,盡管使用的數據和標簽相同:

Unbreakable                 <- 值
    Actors                  <- 分類法
        Bruce Willis        <- 術語
        Samuel L. Jackson   <- 術語
    Director                <- 分類法
        M. Night Shyamalan  <- 術語
    ...
Moonrise Kingdom            <- 值
    Actors                  <- 分類法
        Bruce Willis        <- 術語
        Bill Murray         <- 術語
    Director                <- 分類法
        Wes Anderson        <- 術語
    ...

默認目標

當使用分類法時,Hugo 將自動創建列出所有分類法術語的頁面,以及包含與每個術語關聯的內容列表的單獨頁面。例如,在配置中聲明並在內容 front matter 中使用的 categories 分類法將創建以下頁面:

  • example.com/categories/ 的單個頁面,列出分類法中的所有術語
  • 每個術語的單獨分類法列表頁面(例如,/categories/development/),顯示在任何內容文件的 front matter 中標記為該分類法的所有頁面列表

配置

請參閱 configure taxonomies

為內容分配術語

要為頁面分配一個或多個術語,使用分類法的復數名稱創建 front matter 字段,然後將術語添加到相應的數組。例如:

---
categories:
- Category A
- Category B
tags:
- Tag A
- Tag B
title: Example
---
+++
categories = ['Category A', 'Category B']
tags = ['Tag A', 'Tag B']
title = 'Example'
+++
{
   "categories": [
      "Category A",
      "Category B"
   ],
   "tags": [
      "Tag A",
      "Tag B"
   ],
   "title": "Example"
}

分類權重

taxonomic weight(分類權重)是在 front matter 中定義且對每個分類法唯一�?weight(權重),用於確�?Taxonomy 對象中包含的頁面集合的排序順序�

使用名為 [taxonomy_name]_weight 的 front matter 鍵分配分類權重。

---
tags:
- chemistry
- science
tags_weight: 1000
title: Organic Chemistry
weight: 10
---
+++
tags = ['chemistry', 'science']
tags_weight = 1000
title = 'Organic Chemistry'
weight = 10
+++
{
   "tags": [
      "chemistry",
      "science"
   ],
   "tags_weight": 1000,
   "title": "Organic Chemistry",
   "weight": 10
}

使用上面的 front matter,“Organic Chemistry” 頁面將在 section 和首頁上浮動到列表頂部,並且它將在"chemistry"和"science"術語頁面上沉到列表底部。

元數據

通過在 content 目錄中創建相應的 branch bundle 來顯示每個術語的元數據。

例如,創建"authors"分類法:

taxonomies:
  author: authors
[taxonomies]
  author = 'authors'
{
   "taxonomies": {
      "author": "authors"
   }
}

然後為每個術語創建帶有一個 branch bundle 的內容:

content/
└── authors/
    ├── jsmith/
    │   ├── _index.md
    │   └── portrait.jpg
    └── rjones/
        ├── _index.md
        └── portrait.jpg

然後為每個術語頁面添加 front matter:

---
affiliation: University of Chicago
title: John Smith
---
+++
affiliation = 'University of Chicago'
title = 'John Smith'
+++
{
   "affiliation": "University of Chicago",
   "title": "John Smith"
}

然後創建特定於"authors"分類法的 taxonomy 模板:

layouts/authors/taxonomy.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Data.Terms.Alphabetical }}
    <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
    <p>Affiliation: {{ .Page.Params.Affiliation }}</p>
    {{ with .Page.Resources.Get "portrait.jpg" }}
      {{ with .Fill "100x100" }}
        <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="portrait">
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

在上面的示例中,我們列出每個作者,包括他們的隸屬關系和肖像。

或者創建特定於"authors"分類法的 term 模板:

layouts/authors/term.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  <p>Affiliation: {{ .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 }}

在上面的示例中,我們顯示作者,包括他們的隸屬關系和肖像,然後列出關聯的內容。


Last updated: January 1, 0001
Improve this page