原型
概述
内容文件由 前置 matter 和标记语言组成。标记语言通常是 Markdown,但 Hugo 也支持其他 内容格式 。前置 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"
}
您可以为一个或多个 内容类型 创建原型。例如,为帖子使用一个原型,为其他所有内容使用默认原型:
archetypes/
├── default.md
└── posts.md
查找顺序
Hugo 在项目根目录的 archetypes
目录中查找原型,然后回退到主题或已安装模块中的 archetypes
目录。特定内容类型的原型优先于默认原型。
例如,使用此命令:
hugo new content posts/my-first-post.md
原型查找顺序为:
- archetypes/posts.md
- archetypes/default.md
- themes/my-theme/archetypes/posts.md
- themes/my-theme/archetypes/default.md
如果这些文件都不存在,Hugo 将使用内置的默认原型。
函数和上下文
您可以在原型中使用任何 模板函数 。如上所示,默认原型使用 replace
函数在填充前置 matter 中的标题时将连字符替换为空格。
原型接收以下上下文:
- 日期
- (
string
) 当前日期和时间,格式符合 RFC3339。 - 文件
- (
hugolib.fileInfo
) 返回当前页面的文件信息。参见 详细信息 。 - 类型
- (
string
) 从顶级目录名称推断的 内容类型 ,或由传递给hugo new content
命令的--kind
标志指定。
- 站点
- (
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 }}"
}
包含内容
尽管通常用作前置 matter 模板,但您也可以使用原型来填充内容。
例如,在文档站点中,您可能有一个用于函数的部分(内容类型)。此部分中的每个页面都应遵循相同的格式:简短描述、函数签名、示例和注释。我们可以预先填充页面以提醒内容作者标准格式。
---
date: '{{ .Date }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---
简要描述函数的功能,使用第三人称单数形式的简单现在时。例如:
`someFunction` 返回重复 `n` 次的字符串 `s` 。
## 签名 {#signature}
```text
func someFunction(s string, n int) string
```
## 示例 {#examples}
一个或多个实际示例,每个示例都在一个围栏代码块内。
## 注释 {#notes}
根据需要澄清的其他信息。
尽管您可以在内容主体中包含 模板操作 ,但请记住,Hugo 只会在内容创建时评估这些操作一次。在大多数情况下,请将模板操作放在 模板 中,Hugo 在每次 构建 站点时都会评估这些操作。
叶子捆绑包
您还可以为 叶子捆绑包 创建原型。
例如,在摄影网站中,您可能有一个用于图库的部分(内容类型)。每个图库都是包含内容和图像的叶子捆绑包。
为图库创建一个原型:
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
命令行标志指定原型。
例如,假设您的站点有两个部分:文章和教程。为每个内容类型创建一个原型:
archetypes/
├── articles.md
├── default.md
└── tutorials.md
要使用 articles 原型创建文章:
hugo new content articles/something.md
要使用 tutorials 原型创建文章:
hugo new content --kind tutorials articles/something.md