HUGO

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio Star
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可证
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 入门
    • 本节内容
    • 快速入门
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 配置构建
    • 术语表
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • 前言
    • 构建选项
    • 页面资源
    • 图像处理
    • 短代码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类法
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基础模板
    • 主页模板
    • 单页模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 短代码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 嵌入式模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go 模板
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • 方法
    • 本节内容
    • 持续时间
    • 菜单
    • 菜单项
    • 页面
    • 分页器
    • 页面集合
    • 资源
    • 短代码
    • 站点
    • 分类法
    • 时间
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • 图像
    • 链接
    • 直接传递
    • 表格
  • 短代码
    • 本节内容
    • 评论
    • 详细信息
    • 图
    • Gist
    • 高亮
    • Instagram
    • 参数
    • QR
    • Ref
    • Relref
    • Vimeo
    • X
    • YouTube
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo 管道
    • 本节内容
    • 简介
    • 将 Sass 转译为 CSS
    • PostCSS
    • 后处理
    • JavaScript 构建
    • 资源缩小
    • 连接资源
    • 指纹识别和 SRI 哈希
    • 字符串资源
    • 模板资源
  • CLI
  • 故障排除
    • 本节内容
    • 审计
    • 日志
    • 检查
    • 弃用
    • 性能
    • 常见问题
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • 搜索
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo Deploy
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Azure 静态 Web 应用上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitHub Pages 上托管
    • 在 GitLab Pages 上托管
    • 在 KeyCDN 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
内容管理 基础

构建选项

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

构建选项存储在名为 build 的保留前言对象中,具有以下默认值

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
    always
  • 始终将页面渲染到磁盘。这是默认值。
    link
  • never
    不要将页面渲染到磁盘,但分配 Permalink 和 RelPermalink 值。

never

永远不要将页面渲染到磁盘,并将其从所有页面集合中排除。

任何页面,无论其构建选项如何,始终可以通过使用.Page.GetPage或.Site.GetPage方法获得。

content/
├── headless/
│   ├── a.jpg
│   ├── b.jpg
│   └── index.md  <-- leaf bundle
└── _index.md     <-- home page

示例 – 无头页面

创建一个未发布的页面,其内容和资源可以包含在其他页面中。
     
---
build:
  list: never
  publishResources: false
  render: never
title: Headless page
---
+++
title = 'Headless page'
[build]
  list = 'never'
  publishResources = false
  render = 'never'
+++
{
   "build": {
      "list": "never",
      "publishResources": false,
      "render": "never"
   },
   "title": "Headless page"
}

在前言中设置构建选项

content/headless/index.md
{{ 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

layouts/_default/home.html

  1. 发布的站点将具有以下结构
  2. 在上面的示例中,请注意

Hugo 没有为该页面发布 HTML 文件。

尽管在前言中将 publishResources 设置为 false,但 Hugo 发布了页面资源,因为我们在每个资源上调用了RelPermalink方法。这是预期的行为。

content/
├── headless/
│   ├── note-1/
│   │   ├── a.jpg
│   │   ├── b.jpg
│   │   └── index.md  <-- leaf bundle
│   ├── note-2/
│   │   ├── c.jpg
│   │   ├── d.jpg
│   │   └── index.md  <-- leaf bundle
│   └── _index.md     <-- branch bundle
└── _index.md         <-- home page

示例 – 无头部分

创建一个未发布的部分,其内容和资源可以包含在其他页面中。
     
---
cascade:
- build:
    list: local
    publishResources: false
    render: never
title: Headless section
---
+++
title = 'Headless section'
[[cascade]]
  [cascade.build]
    list = 'local'
    publishResources = false
    render = 'never'
+++
{
   "cascade": [
      {
         "build": {
            "list": "local",
            "publishResources": false,
            "render": "never"
         }
      }
   ],
   "title": "Headless section"
}

在前言中设置构建选项,使用 cascade 关键字将值“级联”到后代页面。

在前言中设置构建选项

content/headless/index.md
{{ 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

layouts/_default/home.html

  1. 发布的站点将具有以下结构
  2. content/headless/_index.md

在上面的前言中,请注意我们将 list 设置为 local,以便将后代页面包含在本地页面集合中。

尽管在前言中将 publishResources 设置为 false,但 Hugo 正确发布了页面资源,因为我们在每个资源上调用了RelPermalink方法。这是预期的行为。

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

示例 – 无头部分

示例 – 不发布列表
     
---
build:
  render: always
cascade:
- build:
    list: local
    publishResources: false
    render: never
title: Glossary
---
+++
title = 'Glossary'
[build]
  render = 'always'
[[cascade]]
  [cascade.build]
    list = 'local'
    publishResources = false
    render = 'never'
+++
{
   "build": {
      "render": "always"
   },
   "cascade": [
      {
         "build": {
            "list": "local",
            "publishResources": false,
            "render": "never"
         }
      }
   ],
   "title": "Glossary"
}

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

content/glossary/_index.md
<dl>
  {{ range .Pages }}
    <dt>{{ .Title }}</dt>
    <dd>{{ .Content }}</dd>
  {{ end }}
</dl>

在主页上包含内容和图像

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

渲染词汇表

layouts/glossary/list.html

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

示例 – 无头页面

示例 – 发布但不列出
     
---
build:
  list: never
  render: never
title: Books
---
+++
title = 'Books'
[build]
  list = 'never'
  render = 'never'
+++
{
   "build": {
      "list": "never",
      "render": "never"
   },
   "title": "Books"
}

在主页上包含内容和图像

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

发布一个章节的后代页面,而不发布章节页面本身。

content/books/_index.md

与其为短代码提供外部文档,不如包含一个“内部”部分,该部分在构建生产站点时会被隐藏。

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: Internal
title = 'Internal'
[[cascade]]
  [cascade._target]
    environment = 'production'
  [cascade.build]
    list = 'never'
    render = 'never'
{
   "cascade": [
      {
         "_target": {
            "environment": "production"
         },
         "build": {
            "list": "never",
            "render": "never"
         }
      }
   ],
   "title": "Internal"
}

生产站点将具有以下结构

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

另请参阅

  • 原型
  • 评论
  • 配置构建
  • 组织
  • 前言

本页内容

  • 示例 – 无头页面
  • 示例 – 无头部分
  • 示例 – 不发布列表
  • 示例 – 发布但不列出
  • 示例 – 有条件地隐藏部分
最后更新:2024 年 2 月 3 日:将指向变量页面的链接替换为指向方法页面的链接 (b8d5383f7)
改进此页面
由 Hugo 作者 提供
Hugo Logo
  • 提交问题
  • 获取帮助
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo 赞助商

您的公司?
 

Hugo 标志的版权归 Steve Francia 2013–2025 所有。

Hugo 地鼠基于 Renée French 的原创作品。

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