HUGO 中文文档

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio Star
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可证
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 快速上手
    • 本节内容
    • 快速入门
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 术语表
    • 配置构建
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • Front matter (前置 matter)
    • 构建选项
    • 页面资源
    • 图片处理
    • 短代码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类法
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学公式
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基模板
    • 首页模板
    • 单个模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 短代码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 内嵌模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • css
    • fmt
    • go 模板
    • hugo
    • js
    • lang
    • openapi3
    • os
    • urls
    • 全局
    • 加密
    • 反射
    • 变形
    • 变换
    • 哈希
    • 图像
    • 图表函数
    • 字符串
    • 安全函数
    • 局部模板函数
    • 数学
    • 数据
    • 时间
    • 模板
    • 比较
    • 类型转换
    • 编码
    • 调试
    • 资源
    • 路径
    • 集合
  • 方法
    • 本节内容
    • Duration
    • Menu
    • Page
    • Pager
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
    • 菜单项
    • 页面
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • Images
    • 链接
    • Passthrough
    • 表格
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo 管道
    • 本节内容
    • 简介
    • 将 Sass 编译为 CSS
    • PostCSS
    • PostProcess
    • JavaScript 构建
    • 资源压缩
    • 连接资产
    • 指纹和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • 命令行界面
  • 故障排除
    • 本节内容
    • Audit
    • 日志记录
    • 检查
    • 弃用
    • 性能
    • FAQs
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • Search
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo 部署
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitLab Pages 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
    • 托管在 Azure 静态 Web 应用上
    • 托管在 GitHub Pages 上
    • 托管在 KeyCDN 上
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
CONTENT MANAGEMENT FUNDAMENTALS

构建选项

构建选项有助于定义 Hugo 在构建站点时必须如何处理给定页面。

构建选项存储在一个名为 build 的保留前置 matter 对象中,其默认值如下:

content/example/index.md
     
---
build:
  list: always
  publishResources: true
  render: always
---
+++
[build]
  list = 'always'
  publishResources = true
  render = 'always'
+++
{
   "build": {
      "list": "always",
      "publishResources": true,
      "render": "always"
   }
}
list
何时将页面包含在页面集合中。指定以下其中一个:
  • always
    将页面包含在 所有 页面集合中。例如, site.RegularPages 、 .Pages 等。这是默认值。
  • local
    将页面包含在 局部 页面集合中。例如, .RegularPages 、 .Pages 等。使用此选项可以创建完全可导航但无头的内容部分。
  • never
    不要将页面包含在 任何 页面集合中。
publishResources
适用于 页面包 ,确定是否发布关联的 页面资源 。指定以下其中一个:
  • true
    始终发布资源。这是默认值。
  • false
    仅在模板中调用其 Permalink 、 RelPermalink 或 Publish 方法时才发布资源。
render
何时渲染页面。指定以下其中一个:
  • always
    始终将页面渲染到磁盘。这是默认值。
  • link
    不要将页面渲染到磁盘,但分配 Permalink 和 RelPermalink 值。
  • never
    从不将页面渲染到磁盘,并将其从所有页面集合中排除。

无论页面的构建选项如何,都可以始终使用 .Page.GetPage 或 .Site.GetPage 方法访问任何页面。

示例 – 无头页面

创建一个未发布的页面,其内容和资源可以包含在其他页面中。

content/
├── headless/
│   ├── a.jpg
│   ├── b.jpg
│   └── index.md  <-- 叶子包
└── _index.md     <-- 首页

在 front matter 中设置构建选项:

content/headless/index.md
     
---
build:
  list: never
  publishResources: false
  render: never
title: 无头页面
---
+++
title = '无头页面'
[build]
  list = 'never'
  publishResources = false
  render = 'never'
+++
{
   "build": {
      "list": "never",
      "publishResources": false,
      "render": "never"
   },
   "title": "无头页面"
}

要在首页上包含内容和图像:

layouts/_default/home.html
{{ with .Site.GetPage "/headless" }}
  {{ .Content }}
  {{ range .Resources.ByType "image" }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

已发布的站点将具有以下结构:

public/
├── headless/
│   ├── a.jpg
│   └── b.jpg
└── index.html

在上面的示例中,请注意:

  1. Hugo 没有为页面发布 HTML 文件。
  2. 尽管在 front matter 中将 publishResources 设置为 false ,但 Hugo 发布了 页面资源 ,因为我们在每个资源上调用了 RelPermalink 方法。这是预期的行为。

示例 – 无头章节

创建一个未发布的章节,其内容和资源可以包含在其他页面中。

content/
├── headless/
│   ├── note-1/
│   │   ├── a.jpg
│   │   ├── b.jpg
│   │   └── index.md  <-- 叶子包
│   ├── note-2/
│   │   ├── c.jpg
│   │   ├── d.jpg
│   │   └── index.md  <-- 叶子包
│   └── _index.md     <-- 分支包
└── _index.md         <-- 首页

在 front matter 中设置构建选项,使用 cascade 关键字将值“级联”到子页面。

content/headless/_index.md
     
---
cascade:
- build:
    list: local
    publishResources: false
    render: never
title: 无头章节
---
+++
title = '无头章节'
[[cascade]]
  [cascade.build]
    list = 'local'
    publishResources = false
    render = 'never'
+++
{
   "cascade": [
      {
         "build": {
            "list": "local",
            "publishResources": false,
            "render": "never"
         }
      }
   ],
   "title": "无头章节"
}

在上面的 front matter 中,请注意,我们已将 list 设置为 local ,以便将子页面包含在局部页面集合中。

要在首页上包含内容和图像:

layouts/_default/home.html
{{ with .Site.GetPage "/headless" }}
  {{ range .Pages }}
    {{ .Content }}
    {{ range .Resources.ByType "image" }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ end }}
  {{ end }}
{{ end }}

已发布的站点将具有以下结构:

public/
├── headless/
│   ├── note-1/
│   │   ├── a.jpg
│   │   └── b.jpg
│   └── note-2/
│       ├── c.jpg
│       └── d.jpg
└── index.html

在上面的示例中,请注意:

  1. Hugo 没有为页面发布 HTML 文件。
  2. 尽管在 front matter 中将 publishResources 设置为 false ,但 Hugo 正确地发布了 页面资源 ,因为我们在每个资源上调用了 RelPermalink 方法。这是预期的行为。

示例 – 不发布的列表

发布一个章节页面,但不发布子页面。例如,创建一个词汇表:

content/
├── glossary/
│   ├── _index.md
│   ├── bar.md
│   ├── baz.md
│   └── foo.md
└── _index.md

在 front matter 中设置构建选项,使用 cascade 关键字将值“级联”到子页面。

content/glossary/_index.md
     
---
build:
  render: always
cascade:
- build:
    list: local
    publishResources: false
    render: never
title: 词汇表
---
+++
title = '词汇表'
[build]
  render = 'always'
[[cascade]]
  [cascade.build]
    list = 'local'
    publishResources = false
    render = 'never'
+++
{
   "build": {
      "render": "always"
   },
   "cascade": [
      {
         "build": {
            "list": "local",
            "publishResources": false,
            "render": "never"
         }
      }
   ],
   "title": "词汇表"
}

渲染词汇表:

layouts/glossary/list.html
<dl>
  {{ range .Pages }}
    <dt>{{ .Title }}</dt>
    <dd>{{ .Content }}</dd>
  {{ end }}
</dl>

已发布的站点将具有以下结构:

public/
├── glossary/
│   └── index.html
└── index.html

示例 – 不列出的发布

发布章节的子页面,但不发布章节页面本身。

content/
├── books/
│   ├── _index.md
│   ├── book-1.md
│   └── book-2.md
└── _index.md

在 front matter 中设置构建选项:

content/books/_index.md
     
---
build:
  list: never
  render: never
title: 书籍
---
+++
title = '书籍'
[build]
  list = 'never'
  render = 'never'
+++
{
   "build": {
      "list": "never",
      "render": "never"
   },
   "title": "书籍"
}

已发布的站点将具有以下结构:

public/
├── books/
│   ├── book-1/
│   │   └── index.html
│   └── book-2/
│       └── index.html
└── index.html

示例 – 有条件地隐藏章节

考虑此示例。一个文档站点拥有一组贡献者,他们可以访问 20 个自定义短代码。每个短代码都带有多个参数,并且需要文档供贡献者在使用时参考。

不要为短代码提供外部文档,而是包含一个在构建生产站点时隐藏的“内部”章节。

content/
├── internal/
│   ├── shortcodes/
│   │   ├── _index.md
│   │   ├── shortcode-1.md
│   │   └── shortcode-2.md
│   └── _index.md
├── reference/
│   ├── _index.md
│   ├── reference-1.md
│   └── reference-2.md
├── tutorials/
│   ├── _index.md
│   ├── tutorial-1.md
│   └── tutorial-2.md
└── _index.md

在 front matter 中设置构建选项,使用 cascade 关键字将值“级联”到子页面,并使用 target 关键字定位生产环境。

content/internal/_index.md.
     
cascade:
- _target:
    environment: production
  build:
    list: never
    render: never
title: 内部
title = '内部'
[[cascade]]
  [cascade._target]
    environment = 'production'
  [cascade.build]
    list = 'never'
    render = 'never'
{
   "cascade": [
      {
         "_target": {
            "environment": "production"
         },
         "build": {
            "list": "never",
            "render": "never"
         }
      }
   ],
   "title": "内部"
}

生产站点将具有以下结构:

public/
├── reference/
│   ├── reference-1/
│   │   └── index.html
│   ├── reference-2/
│   │   └── index.html
│   └── index.html
├── tutorials/
│   ├── tutorial-1/
│   │   └── index.html
│   ├── tutorial-2/
│   │   └── index.html
│   └── index.html
└── index.html

See also

  • Front matter (前置 matter)
  • 组织
  • 分类法
  • 原型
  • 相关内容

On this page

  • 示例 – 无头页面
  • 示例 – 无头章节
  • 示例 – 不发布的列表
  • 示例 – 不列出的发布
  • 示例 – 有条件地隐藏章节
Last updated: January 10, 2025: 添加 gtm 谷歌代码管理 (6220bf5)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays
 

Hugo Sponsors

Your Company?
 

The Hugo logos are copyright © Steve Francia 2013–2025.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 快速上手
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • Hugo 模块
  • Hugo 管道
  • 命令行界面
  • 故障排除
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护