Debian Codegraph @

docs/content/en/content-management/build-options.md

raw
1 ---
2 title: Build options
3 description: Build options help define how Hugo must treat a given page when building the site.
4 categories: []
5 keywords: []
6 aliases: [/content/build-options/]
7 ---
8
9 Build options are stored in a reserved front matter object named `build` with these defaults:
10
11 {{< code-toggle file=content/example/index.md fm=true >}}
12 [build]
13 list = 'always'
14 publishResources = true
15 render = 'always'
16 {{< /code-toggle >}}
17
18 `list`
19 : When to include the page within page collections. Specify one of:
20
21 - `always`: Include the page in _all_ page collections. For example, `site.RegularPages`, `.Pages`, etc. This is the default value.
22 - `local`: Include the page in _local_ page collections. For example, `.RegularPages`, `.Pages`, etc. Use this option to create fully navigable but headless content sections.
23 - `never`: Do not include the page in _any_ page collection.
24
25 `publishResources`
26 : Applicable to [page bundles][], determines whether to publish the associated [page resources][]. Specify one of:
27
28 - `true`: Always publish resources. This is the default value.
29 - `false`: Only publish a resource when invoking its [`Permalink`][], [`RelPermalink`][], or [`Publish`][] method within a template.
30
31 `render`
32 : When to render the page. Specify one of:
33
34 - `always`: Always render the page to disk. This is the default value.
35 - `link`: Do not render the page to disk, but assign `Permalink` and `RelPermalink` values.
36 - `never`: Never render the page to disk, and exclude it from all page collections.
37
38 > [!NOTE]
39 > Any page, regardless of its build options, will always be available by using the [`.Page.GetPage`][] or [`.Site.GetPage`][] method.
40
41 ## Headless page
42
43 Create a unpublished page whose content and resources can be included in other pages.
44
45 ```tree
46 content/
47 ├── headless/
48 │ ├── a.jpg
49 │ ├── b.jpg
50 │ └── index.md <-- leaf bundle
51 └── _index.md <-- home page
52 ```
53
54 Set the build options in front matter:
55
56 {{< code-toggle file=content/headless/index.md fm=true >}}
57 title = 'Headless page'
58 [build]
59 list = 'never'
60 publishResources = false
61 render = 'never'
62 {{< /code-toggle >}}
63
64 To include the content and images on the home page:
65
66 ```go-html-template {file="layouts/home.html"}
67 {{ with .Site.GetPage "/headless" }}
68 {{ .Content }}
69 {{ range .Resources.ByType "image" }}
70 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
71 {{ end }}
72 {{ end }}
73 ```
74
75 The published site will have this structure:
76
77 ```tree
78 public/
79 ├── headless/
80 │ ├── a.jpg
81 │ └── b.jpg
82 └── index.html
83 ```
84
85 In the example above, note that:
86
87 1. Hugo did not publish an HTML file for the page.
88 1. Despite setting `publishResources` to `false` in front matter, Hugo published the [page resources][] because we invoked the [`RelPermalink`][] method on each resource. This is the expected behavior.
89
90 ## Headless section
91
92 Create a unpublished section whose content and resources can be included in other pages.
93
94 ```tree
95 content/
96 ├── headless/
97 │ ├── note-1/
98 │ │ ├── a.jpg
99 │ │ ├── b.jpg
100 │ │ └── index.md <-- leaf bundle
101 │ ├── note-2/
102 │ │ ├── c.jpg
103 │ │ ├── d.jpg
104 │ │ └── index.md <-- leaf bundle
105 │ └── _index.md <-- branch bundle
106 └── _index.md <-- home page
107 ```
108
109 Set the build options in front matter, using the `cascade` keyword to "cascade" the values down to descendant pages.
110
111 {{< code-toggle file=content/headless/_index.md fm=true >}}
112 title = 'Headless section'
113 [[cascade]]
114 [cascade.build]
115 list = 'local'
116 publishResources = false
117 render = 'never'
118 {{< /code-toggle >}}
119
120 In the front matter above, note that we have set `list` to `local` to include the descendant pages in local page collections.
121
122 To include the content and images on the home page:
123
124 ```go-html-template {file="layouts/home.html"}
125 {{ with .Site.GetPage "/headless" }}
126 {{ range .Pages }}
127 {{ .Content }}
128 {{ range .Resources.ByType "image" }}
129 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
130 {{ end }}
131 {{ end }}
132 {{ end }}
133 ```
134
135 The published site will have this structure:
136
137 ```tree
138 public/
139 ├── headless/
140 │ ├── note-1/
141 │ │ ├── a.jpg
142 │ │ └── b.jpg
143 │ └── note-2/
144 │ ├── c.jpg
145 │ └── d.jpg
146 └── index.html
147 ```
148
149 In the example above, note that:
150
151 1. Hugo did not publish an HTML file for the page.
152 1. Despite setting `publishResources` to `false` in front matter, Hugo correctly published the [page resources][] because we invoked the [`RelPermalink`][] method on each resource. This is the expected behavior.
153
154 ## List without publishing
155
156 Publish a section page without publishing the descendant pages. For example, to create a glossary:
157
158 ```tree
159 content/
160 ├── glossary/
161 │ ├── _index.md
162 │ ├── bar.md
163 │ ├── baz.md
164 │ └── foo.md
165 └── _index.md
166 ```
167
168 Set the build options in front matter, using the `cascade` keyword to "cascade" the values down to descendant pages.
169
170 {{< code-toggle file=content/glossary/_index.md fm=true >}}
171 title = 'Glossary'
172 [build]
173 render = 'always'
174 [[cascade]]
175 [cascade.build]
176 list = 'local'
177 publishResources = false
178 render = 'never'
179 {{< /code-toggle >}}
180
181 To render the glossary:
182
183 ```go-html-template {file="layouts/glossary/section.html"}
184 <dl>
185 {{ range .Pages }}
186 <dt>{{ .Title }}</dt>
187 <dd>{{ .Content }}</dd>
188 {{ end }}
189 </dl>
190 ```
191
192 The published site will have this structure:
193
194 ```tree
195 public/
196 ├── glossary/
197 │ └── index.html
198 └── index.html
199 ```
200
201 ## Publish without listing
202
203 Publish a section's descendant pages without publishing the section page itself.
204
205 ```tree
206 content/
207 ├── books/
208 │ ├── _index.md
209 │ ├── book-1.md
210 │ └── book-2.md
211 └── _index.md
212 ```
213
214 Set the build options in front matter:
215
216 {{< code-toggle file=content/books/_index.md fm=true >}}
217 title = 'Books'
218 [build]
219 render = 'never'
220 list = 'never'
221 {{< /code-toggle >}}
222
223 The published site will have this structure:
224
225 ```tree
226 public/
227 ├── books/
228 │ ├── book-1/
229 │ │ └── index.html
230 │ └── book-2/
231 │ └── index.html
232 └── index.html
233 ```
234
235 ## Conditionally hide a section
236
237 Consider this example. A documentation site has a team of contributors with access to 20 custom shortcodes. Each shortcode takes several arguments, and requires documentation for the contributors to reference when using them.
238
239 Instead of external documentation for the shortcodes, include an `internal` section that is hidden when building the production site.
240
241 ```tree
242 content/
243 ├── internal/
244 │ ├── shortcodes/
245 │ │ ├── _index.md
246 │ │ ├── shortcode-1.md
247 │ │ └── shortcode-2.md
248 │ └── _index.md
249 ├── reference/
250 │ ├── _index.md
251 │ ├── reference-1.md
252 │ └── reference-2.md
253 ├── tutorials/
254 │ ├── _index.md
255 │ ├── tutorial-1.md
256 │ └── tutorial-2.md
257 └── _index.md
258 ```
259
260 Set the build options in front matter, using the `cascade` keyword to "cascade" the values down to descendant pages, and use the `target` keyword to target the production environment.
261
262 {{< code-toggle file=content/internal/_index.md >}}
263 title = 'Internal'
264 [[cascade]]
265 [cascade.build]
266 render = 'never'
267 list = 'never'
268 [cascade.target]
269 environment = 'production'
270 {{< /code-toggle >}}
271
272 The production site will have this structure:
273
274 ```tree
275 public/
276 ├── reference/
277 │ ├── reference-1/
278 │ │ └── index.html
279 │ ├── reference-2/
280 │ │ └── index.html
281 │ └── index.html
282 ├── tutorials/
283 │ ├── tutorial-1/
284 │ │ └── index.html
285 │ ├── tutorial-2/
286 │ │ └── index.html
287 │ └── index.html
288 └── index.html
289 ```
290
291 [`.Page.GetPage`]: /methods/page/getpage/
292 [`.Site.GetPage`]: /methods/site/getpage/
293 [`Permalink`]: /methods/resource/permalink/
294 [`Publish`]: /methods/resource/publish/
295 [`RelPermalink`]: /methods/resource/relpermalink/
296 [page bundles]: /content-management/page-bundles/
297 [page resources]: /content-management/page-resources/