Debian Codegraph @

docs/content/en/content-management/page-resources.md

raw
1 ---
2 title: Page resources
3 description: Use page resources to logically associate assets with a page.
4 categories: []
5 keywords: []
6 ---
7
8 Page resources are only accessible from [page bundles][], those directories with `index.md` or`_index.md` files at their root. Page resources are only available to the page with which they are bundled.
9
10 In this example, `first-post` is a page bundle with access to 10 page resources including audio, data, documents, images, and video. Although `second-post` is also a page bundle, it has no page resources and is unable to directly access the page resources associated with `first-post`.
11
12 ```tree
13 content
14 └── post
15 ├── first-post
16 │ ├── images
17 │ │ ├── a.jpg
18 │ │ ├── b.jpg
19 │ │ └── c.jpg
20 │ ├── index.md (root of page bundle)
21 │ ├── latest.html
22 │ ├── manual.json
23 │ ├── notice.md
24 │ ├── office.mp3
25 │ ├── pocket.mp4
26 │ ├── rating.pdf
27 │ └── safety.txt
28 └── second-post
29 └── index.md (root of page bundle)
30 ```
31
32 ## Examples
33
34 Use any of these methods on a `Page` object to capture page resources:
35
36 - [`Resources.ByType`][]
37 - [`Resources.Get`][]
38 - [`Resources.GetMatch`][]
39 - [`Resources.Match`][]
40
41 Once you have captured a resource, use any of the applicable [`Resource`][] methods to return a value or perform an action.
42
43 The following examples assume this content structure:
44
45 ```tree
46 content/
47 └── example/
48 ├── data/
49 │ └── books.json <-- page resource
50 ├── images/
51 │ ├── a.jpg <-- page resource
52 │ └── b.jpg <-- page resource
53 ├── snippets/
54 │ └── text.md <-- page resource
55 └── index.md
56 ```
57
58 Render a single image, and throw an error if the file does not exist:
59
60 ```go-html-template
61 {{ $path := "images/a.jpg" }}
62 {{ with .Resources.Get $path }}
63 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
64 {{ else }}
65 {{ errorf "Unable to get page resource %q" $path }}
66 {{ end }}
67 ```
68
69 Render all images, resized to 300 px wide:
70
71 ```go-html-template
72 {{ range .Resources.ByType "image" }}
73 {{ with .Resize "300x" }}
74 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
75 {{ end }}
76 {{ end }}
77 ```
78
79 Render the markdown snippet:
80
81 ```go-html-template
82 {{ with .Resources.Get "snippets/text.md" }}
83 {{ .Content }}
84 {{ end }}
85 ```
86
87 List the titles in the data file, and throw an error if the file does not exist.
88
89 ```go-html-template
90 {{ $path := "data/books.json" }}
91 {{ with .Resources.Get $path }}
92 {{ with . | transform.Unmarshal }}
93 <p>Books:</p>
94 <ul>
95 {{ range . }}
96 <li>{{ .title }}</li>
97 {{ end }}
98 </ul>
99 {{ end }}
100 {{ else }}
101 {{ errorf "Unable to get page resource %q" $path }}
102 {{ end }}
103 ```
104
105 ## Metadata
106
107 The page resources' metadata is managed from the corresponding page's front matter with an array parameter named `resources`.
108
109 > [!NOTE]
110 > Resources of type `page` get `Title` etc. from their own front matter.
111
112 `src`
113 : (`string`) Required. A [glob pattern](g) matching one or more page resources by file path, relative to the page bundle. Matching is case-insensitive. When the pattern matches multiple resources, the same metadata is applied to each.
114
115 `name`
116 : (`string`) Sets the value returned by [`Name`][]. Supports the [`:counter`](#the-counter-placeholder) placeholder. After assignment, use `name`, not the original file path, with [`Resources.Get`][], [`Resources.Match`][], and [`Resources.GetMatch`][].
117
118 `title`
119 : (`string`) Sets the value returned by [`Title`][]. Supports the [`:counter`](#the-counter-placeholder) placeholder.
120
121 `params`
122 : (`map`) A map of custom key-value pairs. When multiple array entries match the same resource, their `params` maps are merged; later entries take precedence for duplicate keys.
123
124 ### Resources metadata example
125
126 <!-- markdownlint-disable MD007 MD032 -->
127 {{< code-toggle file=content/example.md fm=true >}}
128 title: Application
129 date: 2018-01-25
130 resources:
131 - src: images/sunset.jpg
132 name: header
133 - src: documents/photo_specs.pdf
134 title: Photo Specifications
135 - src: documents/guide.pdf
136 title: Instruction Guide
137 - src: documents/checklist.pdf
138 title: Document Checklist
139 - src: documents/payment.docx
140 title: Proof of Payment
141 - src: "**.pdf"
142 name: pdf-file-:counter
143 params:
144 icon: pdf
145 - src: "**.docx"
146 params:
147 icon: word
148 {{</ code-toggle >}}
149 <!-- markdownlint-enable MD007 MD032 -->
150
151 From the example above:
152
153 - `sunset.jpg` will receive a new `Name` and can now be found with `.GetMatch "header"`.
154 - `documents/photo_specs.pdf`, `documents/guide.pdf`, `documents/checklist.pdf`, and `documents/payment.docx` will get `Title` as set by `title`.
155 - All `PDF` files will get the `pdf` icon and a new `Name`. The `name` parameter contains a special placeholder [`:counter`](#the-counter-placeholder), so the `Name` will be `pdf-file-1`, `pdf-file-2`, `pdf-file-3`.
156 - All `.docx` files will get the `word` icon.
157
158 > [!NOTE]
159 > For `name` and `title`, the first matching array entry wins; later matches are ignored. For `params`, all matching entries contribute; later entries take precedence for duplicate keys. Place more specific `src` patterns before broader wildcards to control which `name` and `title` values are applied.
160
161 ### The counter placeholder
162
163 The `:counter` is a special placeholder recognized in `name` and `title` parameters `resources`.
164
165 Each unique `src` pattern maintains independent counters for `name` and `title`, each starting at 1 with the first matching resource.
166
167 For example, if a bundle has the resources `photo_specs.pdf`, `other_specs.pdf`, `guide.pdf` and `checklist.pdf`, and the front matter has specified the `resources` as:
168
169 {{< code-toggle file=content/inspections/engine/index.md fm=true >}}
170 title = 'Engine inspections'
171 [[resources]]
172 src = '*specs.pdf'
173 title = 'Specification #:counter'
174 [[resources]]
175 src = '**.pdf'
176 name = 'pdf-file-:counter.pdf'
177 {{</ code-toggle >}}
178
179 the `Name` and `Title` will be assigned to the resource files as follows:
180
181 | Resource file | `Name` | `Title` |
182 |------------------|--------------------|----------------------|
183 | checklist.pdf | `"pdf-file-1.pdf"` | `"checklist.pdf"` |
184 | guide.pdf | `"pdf-file-2.pdf"` | `"guide.pdf"` |
185 | other\_specs.pdf | `"pdf-file-3.pdf"` | `"Specification #1"` |
186 | photo\_specs.pdf | `"pdf-file-4.pdf"` | `"Specification #2"` |
187
188 ## Multilingual
189
190 By default, with a multilingual single-host project, Hugo does not duplicate shared page during the build.
191
192 > [!NOTE]
193 > This behavior is limited to Markdown content. Shared page resources for other [content formats][] are copied into each language bundle.
194
195 Consider this project configuration:
196
197 {{< code-toggle file=hugo >}}
198 defaultContentLanguage = 'de'
199 defaultContentLanguageInSubdir = true
200
201 [languages.de]
202 label = 'Deutsch'
203 locale = 'de-DE'
204 weight = 1
205
206 [languages.en]
207 label = 'English'
208 locale = 'en-US'
209 weight = 2
210 {{< /code-toggle >}}
211
212 And this content:
213
214 ```tree
215 content/
216 └── my-bundle/
217 ├── a.jpg <-- shared page resource
218 ├── b.jpg <-- shared page resource
219 ├── c.de.jpg
220 ├── c.en.jpg
221 ├── index.de.md
222 └── index.en.md
223 ```
224
225 Hugo places the shared resources in the page bundle for the default content language:
226
227 ```tree
228 public/
229 ├── de/
230 │ ├── my-bundle/
231 │ │ ├── a.jpg <-- shared page resource
232 │ │ ├── b.jpg <-- shared page resource
233 │ │ ├── c.de.jpg
234 │ │ └── index.html
235 │ └── index.html
236 ├── en/
237 │ ├── my-bundle/
238 │ │ ├── c.en.jpg
239 │ │ └── index.html
240 │ └── index.html
241 └── index.html
242 ```
243
244 This approach reduces build times, storage requirements, bandwidth consumption, and deployment times, ultimately reducing cost.
245
246 > [!IMPORTANT]
247 > To resolve Markdown link and image destinations to the correct location, you must use link and image render hooks that capture the page resource with the [`Resources.Get`][] method, and then invoke its [`RelPermalink`][] method.
248 >
249 > In its default configuration, Hugo automatically uses the [embedded link render hook][] and the [embedded image render hook][] for multilingual single-host projects, specifically when the [duplication of shared page resources][] feature is disabled. This is the default behavior for such projects. If custom link or image render hooks are defined by your project, modules, or themes, these will be used instead.
250 >
251 > You can also configure Hugo to `always` use the embedded link or image render hook, use it only as a `fallback`, or `never` use it. See [details][].
252
253 Although duplicating shared page resources is inefficient, you can enable this feature in your project configuration if desired:
254
255 {{< code-toggle file=hugo >}}
256 [markup.goldmark]
257 duplicateResourceFiles = true
258 {{< /code-toggle >}}
259
260 [`Name`]: /methods/resource/name/
261 [`RelPermalink`]: /methods/resource/relpermalink/
262 [`Resource`]: /methods/resource/
263 [`Resources.ByType`]: /methods/page/resources#bytype
264 [`Resources.GetMatch`]: /methods/page/resources#getmatch
265 [`Resources.Get`]: /methods/page/resources/#get
266 [`Resources.Match`]: /methods/page/resources#match
267 [`Title`]: /methods/resource/title/
268 [content formats]: /content-management/formats/
269 [details]: /configuration/markup/#renderhookslinkuseembedded
270 [duplication of shared page resources]: /configuration/markup/#duplicateresourcefiles
271 [embedded image render hook]: /render-hooks/images/#embedded
272 [embedded link render hook]: /render-hooks/links/#embedded
273 [page bundles]: /content-management/page-bundles/