原型
概述
内容文件由前言和标记组成。标记通常是 Markdown,但 Hugo 也支持其他内容格式。前言可以是 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
函数在填充前言中的标题时将连字符替换为空格。
原型接收以下上下文
- 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 }}"
}
包含内容
虽然通常用作前言模板,但您也可以使用原型来填充内容。
例如,在文档站点中,您可能有一个用于函数的节(内容类型)。此节中的每个页面都应遵循相同的格式:简短描述、函数签名、示例和注释。我们可以预先填充页面以提醒内容作者标准格式。
---
date: '{{ .Date }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---
A brief description of what the function does, using simple present tense in the third person singular form. For example:
`someFunction` returns the string `s` repeated `n` times.
## Signature
```text
func someFunction(s string, n int) string
```
## Examples
One or more practical examples, each within a fenced code block.
## Notes
Additional information to clarify as needed.
虽然您可以在内容正文中包含模板操作,但请记住,Hugo 只会在内容创建时评估这些操作一次。在大多数情况下,请将模板操作放置在模板中,在该模板中,Hugo 每次构建站点时都会评估操作。
叶子包
您还可以为叶子包创建原型。
例如,在摄影站点中,您可能有一个用于画廊的节(内容类型)。每个画廊都是带有内容和图像的叶子包。
为画廊创建原型
archetypes/
├── galleries/
│ ├── images/
│ │ └── .gitkeep
│ └── index.md <-- same format as 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
要使用文章原型创建文章
hugo new content articles/something.md
要使用教程原型创建文章
hugo new content --kind tutorials articles/something.md