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