Hugo 原型(Archetypes)
概述
內容文件由 front matter 和標記組成。標記通常是 Markdown,但 Hugo 也支持其他 內容格式。front matter 可以是 TOML、YAML 或 JSON。
hugo new content 命令使用原型作為模板在 content 目錄中創建新文件。這是默認原型:
---
date: '{{ .Date }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---+++
date = '{{ .Date }}'
draft = true
title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
+++{
"date": "{{ .Date }}",
"draft": true,
"title": "{{ replace .File.ContentBaseName `-` ` ` | title }}"
}
當你創建新內容時,Hugo 會評估原型內的 模板動作。例如:
hugo new content posts/my-first-post.md使用上面顯示的默認原型,Hugo 會創建此內容文件:
---
date: '2023-08-24T11:49:46-07:00'
draft: true
title: My First Post
---+++
date = '2023-08-24T11:49:46-07:00'
draft = true
title = 'My First Post'
+++{
"date": "2023-08-24T11:49:46-07:00",
"draft": true,
"title": "My First Post"
}
你可以為一種或多種 內容類型 創建原型。例如,為 posts 使用一個原型,為其他所有內容使用默認原型:
archetypes/
├── default.md
└── posts.md查找順序
Hugo 在項目根目錄的 archetypes 目錄中查找原型,並回退到主題或已安裝模塊中的 archetypes 目錄。特定內容類型的原型優先於默認原型。
例如,如果你啟用了名為 my-theme 的主題並運行此命令:
hugo new content posts/my-first-post.md原型查找順序為:
archetypes/posts.mdthemes/my-theme/archetypes/posts.mdarchetypes/default.mdthemes/my-theme/archetypes/default.md
如果這些都不存在,Hugo 會使用內置的默認原型。
函數和上下文
你可以在原型中使用任何模板 函數。如上所示,默認原型使用 replace 函數將連字符替換為空格,以填充 front matter 中的標題。
原型接收以下 上下文:
- Date
- (
string) 當前日期和時間,按照 RFC3339 格式化。 - File
- (
hugolib.fileInfo) 返回當前頁面的文件信息。參見 詳情。 - Type
- (
string) 內容類型 從頂層目錄名推斷,或由傳遞給hugo new content命令的--kind標志指定。 - Site
- (
page.Site) 當前站點對象。參見 詳情。
日期格式
要插入不同格式的日期和時間,使用 time.Now 函數:
---
date: '{{ time.Now.Format "2006-01-02" }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---+++
date = '{{ time.Now.Format "2006-01-02" }}'
draft = true
title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
+++{
"date": "{{ time.Now.Format \"2006-01-02\" }}",
"draft": true,
"title": "{{ replace .File.ContentBaseName `-` ` ` | title }}"
}
包含內容
雖然原型通常用作 front matter 模板,但也可以用於填充內容。
例如,在文檔站點中,你可能有一個用於函數的 section(內容類型)。此 section 中的每個頁面都應遵循相同的格式:簡要描述、函數簽名、示例和說明。我們可以預填充頁面以提醒內容作者標准格式。
---
date: '{{ .Date }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---
簡要描述函數的作用,使用一般現在時第三人稱單數形式。例如:
`someFunction` 返回字符串 `s` 重復 `n` 次的結果。
## 簽名
```text
func someFunction(s string, n int) string
```
## 示例
一個或多個實際示例,每個都在圍欄代碼塊中。
## 說明
根據需要澄清的附加信息。雖然你可以在內容主體中包含 模板動作,但請記住 Hugo 只在創建內容時評估一次。在大多數情況下,將模板動作放在 模板 中,這樣 Hugo 會在每次 構建 站點時評估這些動作。
葉 bundles
你也可以為 葉 bundles 創建原型。
例如,在攝影站點中,你可能有一個用於畫廊的 section(內容類型)。每個畫廊都是帶有內容和圖像的葉 bundle。
為畫廊創建原型:
archetypes/
├── galleries/
│ ├── images/
│ │ └── .gitkeep
│ └── index.md <-- 與 default.md 格式相同
└── default.md原型內的子目錄必須至少包含一個文件。沒有文件,Hugo 在創建新內容時不會創建子目錄。文件的大小和名稱無關緊要。上面的示例包含一個 .gitkeep 文件,這是一個空文件,通常用於在 Git 倉庫中保留原本為空的目錄。
創建新畫廊:
hugo new galleries/bryce-canyon這將生成:
content/
├── galleries/
│ └── bryce-canyon/
│ ├── images/
│ │ └── .gitkeep
│ └── index.md
└── _index.md指定原型
使用 --kind 命令行標志在創建內容時指定原型。
例如,假設你的站點有兩個 section:articles 和 tutorials。為每種內容類型創建原型:
archetypes/
├── articles.md
├── default.md
└── tutorials.md使用 articles 原型創建 article:
hugo new content articles/something.md使用 tutorials 原型創建 article:
hugo new content --kind tutorials articles/something.md