页面Bundle
4 分钟阅读
Page Bundles - 页面Bundle
https://gohugo.io/content-management/page-bundles/
使用页面 Bundle 进行内容组织
页面 Bundle 是一种分组页面资源的方式。
页面 Bundle 可以是以下之一:
- 叶子Bundle (叶子表示它没有子级)
- 分支Bundle (home page,section,taxonomy terms,taxonomy list)
| 叶子 Bundle | 分支 Bundle | |
|---|---|---|
| 用法 | 单个页面内容和附件的集合 | 用于section页面(home page,section,taxonomy terms,taxonomy list)的附件集合 |
| 索引文件名 | index.md 1 | _index.md 1 |
| 允许的资源 | 页面和非页面类型(如图像、PDF 等) | 仅允许非页面类型(如图像、PDF 等) |
| 资源可以存放在哪里? | 在叶子 Bundle 目录中的任何目录级别。 | 仅在分支 Bundle 目录的目录级别中,即包含 _index.md 的目录(参考)。 |
| 布局类型 | single | list |
| 嵌套 | 不允许在其下方嵌套更多的 Bundle | 允许在其下方嵌套叶子或分支 Bundle |
| 示例 | content/posts/my-post/index.md | content/posts/_index.md |
| 非索引页面文件中的内容… | 仅作为页面资源访问 | 仅作为常规页面访问 |
叶子Bundles
叶子 Bundle 是 content/ 目录中任何层次结构中包含 index.md 文件的目录。
叶子 Bundle 组织示例:
| |
In the above example content/ directory, there are four leaf bundles:
在上面的示例中,content/ 目录中有四个叶子 Bundle:
about这个叶子 Bundle 在根级别(直接在
content目录下)并且只有index.md。my-post这个叶子 Bundle 有
index.md、另外两个内容 Markdown 文件和两个图像文件。image1, image2:这些图像是
my-post的页面资源,仅在my-post/index.md资源中可用。content1, content2: These content files are page resources of
my-postand only available inmy-post/index.mdresources. They will not be rendered as individual pages. 这些内容文件是 my-post 的页面资源,仅在 my-post/index.md 资源中可用。它们不会被渲染为单独的页面。my-other-post这个叶子 Bundle 只有
index.md。another-leaf-bundle这个叶子 Bundle 被嵌套在几个目录下。此 Bundle 也只有
index.md。
创建叶子 Bundle 的层次深度不重要,只要它不在另一个叶子 Bundle 中即可。
Headless Bundle
无头Bundle是一种配置为不在任何地方发布的Bundle:
- 它将没有永久链接(
Permalink)和public/中的渲染HTML。 - 它不会成为
.Site.RegularPages等的一部分。
但是,您可以通过.Site.GetPage获取它。以下是一个示例:
| |
在此示例中,我们假设some-headless-bundle是一个包含一个或多个页面资源的无头Bundle,其.Name与"author*"匹配。
上面示例的说明:
- 获取
some-headless-bundle页面"object"。 - 使用
.Resources.Match收集此页面Bundle中与"author*"匹配的资源片段。 - 循环遍历嵌套页面的切片,并输出它们的
.Title和.Content。
通过在index.md的前置元数据中添加以下内容,可以将一个叶子Bundle变为无头Bundle:
content/headless/index.md
=== “yaml”
```yaml
---
headless: true
---
```
=== “toml”
```toml
+++
headless = true
+++
```
=== “json”
```json
{
"headless": true
}
```
此类无头页面Bundle有许多用例:
- 共享媒体库
- 可重复使用的页面内容"snippets(片段)"
分支Bundle
分支Bundle是位于content/目录中任何层次结构中的任何目录,其中至少包含一个_index.md文件。
这个_index.md也可以直接在content/目录下。
这里以
md(markdown)仅作为示例。只要它是Hugo可识别的内容类型,您可以使用任何文件类型作为内容资源。
分支Bundle组织示例
| |
在上面的content/目录示例中,有两个分支Bundle(和一个叶子Bundle):
branch-bundle-1该分支Bundle有
_index.md,另外两个内容Markdown文件和两个图像文件。branch-bundle-2该分支Bundle有
_index.md和一个嵌套的叶子Bundle。
创建分支Bundle的层次深度不重要。
.md扩展名仅作为示例。扩展名可以是.html,.json或任何有效的MIME类型。 ↩︎ ↩︎