Debian Codegraph @

docs/content/en/content-management/content-adapters.md

raw
1 ---
2 title: Content adapters
3 description: Create content adapters to dynamically add content when building your project.
4 categories: []
5 keywords: []
6 ---
7
8 ## Overview
9
10 A content adapter is a template that dynamically creates pages when building a site. For example, use a content adapter to create pages from a remote data source such as JSON, TOML, YAML, or XML.
11
12 Unlike templates that reside in the `layouts` directory, content adapters reside in the `content` directory, no more than one per directory per language. When a content adapter creates a page, the page's [logical path](g) will be relative to the content adapter.
13
14 ```tree
15 content/
16 ├── articles/
17 │ ├── _index.md
18 │ ├── article-1.md
19 │ └── article-2.md
20 ├── books/
21 │ ├── _content.gotmpl <-- content adapter
22 │ └── _index.md
23 └── films/
24 ├── _content.gotmpl <-- content adapter
25 └── _index.md
26 ```
27
28 Each content adapter is named `_content.gotmpl` and uses the same [syntax][] as templates in the `layouts` directory. You can use any of the [template functions][] within a content adapter, as well as the methods described below.
29
30 ## Methods
31
32 Use these methods within a content adapter.
33
34 `AddPage`
35 : Adds a page to the site.
36
37 ```go-html-template {file="content/books/_content.gotmpl"}
38 {{ $content := dict
39 "mediaType" "text/markdown"
40 "value" "The _Hunchback of Notre Dame_ was written by Victor Hugo."
41 }}
42 {{ $page := dict
43 "content" $content
44 "kind" "page"
45 "path" "the-hunchback-of-notre-dame"
46 "title" "The Hunchback of Notre Dame"
47 }}
48 {{ .AddPage $page }}
49 ```
50
51 `AddResource`
52 : Adds a page resource to the site.
53
54 ```go-html-template {file="content/books/_content.gotmpl"}
55 {{ with resources.Get "images/a.jpg" }}
56 {{ $content := dict
57 "mediaType" .MediaType.Type
58 "value" .
59 }}
60 {{ $resource := dict
61 "content" $content
62 "path" "the-hunchback-of-notre-dame/cover.jpg"
63 }}
64 {{ $.AddResource $resource }}
65 {{ end }}
66 ```
67
68 Then retrieve the new page resource with something like:
69
70 ```go-html-template {file="layouts/page.html"}
71 {{ with .Resources.Get "cover.jpg" }}
72 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
73 {{ end }}
74 ```
75
76 `Site`
77 : (`Site`) Returns the site to which the pages will be added.
78
79 ```go-html-template {file="content/books/_content.gotmpl"}
80 {{ .Site.Title }}
81 ```
82
83 > [!NOTE]
84 > The `Site` object is not fully initialized while Hugo executes a content adapter.
85 > Methods that depend on built pages, such as `Site.Pages`, are unavailable at this stage and return an error.
86
87 `Store`
88 : (`maps.Scratch`) Returns a persistent data structure for storing and manipulating keyed values. The main use case for this is to transfer values between executions when [EnableAllLanguages](#enablealllanguages) is set. See [examples][].
89
90 ```go-html-template {file="content/books/_content.gotmpl"}
91 {{ .Store.Set "key" "value" }}
92 {{ .Store.Get "key" }}
93 ```
94
95 `EnableAllLanguages`
96 : By default, Hugo executes the content adapter only once for the first matching site in the [sites matrix](g). Use this method to expand execution to all languages while maintaining the current role and version.
97
98 For more fine-grained control, define a `sites.matrix` in front matter or in a content mount.
99
100 ```go-html-template {file="content/books/_content.gotmpl"}
101 {{ .EnableAllLanguages }}
102 {{ $content := dict
103 "mediaType" "text/markdown"
104 "value" "The _Hunchback of Notre Dame_ was written by Victor Hugo."
105 }}
106 {{ $page := dict
107 "content" $content
108 "kind" "page"
109 "path" "the-hunchback-of-notre-dame"
110 "title" "The Hunchback of Notre Dame"
111 }}
112 {{ .AddPage $page }}
113 ```
114
115 `EnableAllDimensions`
116 : By default, Hugo executes the content adapter only once for the first matching site in the [sites matrix](g). Use this method to expand execution to every possible combination of language, version, and role.
117
118 For more fine-grained control, define a `sites.matrix` in front matter or in a content mount.
119
120 ## Page map
121
122 Set any [front matter field][] in the map passed to the [`AddPage`](#addpage) method, excluding `markup`. Instead of setting the `markup` field, specify the `content.mediaType` as described below.
123
124 This table describes the fields most commonly passed to the `AddPage` method.
125
126 Key|Description|Required
127 :--|:--|:-:
128 `content.mediaType`|The content [media type][]. Default is `text/markdown`. See [content formats][] for examples.|&nbsp;
129 `content.value`|The content value as a string.|&nbsp;
130 `dates.date`|The page creation date as a `time.Time` value.|&nbsp;
131 `dates.expiryDate`|The page expiry date as a `time.Time` value.|&nbsp;
132 `dates.lastmod`|The page last modification date as a `time.Time` value.|&nbsp;
133 `dates.publishDate`|The page publication date as a `time.Time` value.|&nbsp;
134 `params`|A map of page parameters.|&nbsp;
135 `path`|The page's [logical path](g) relative to the content adapter. Do not include a leading slash or file extension.|:heavy_check_mark:
136 `title`|The page title.|&nbsp;
137
138 > [!NOTE]
139 > While `path` is the only required field, we recommend setting `title` as well.
140 >
141 > When setting the `path`, Hugo transforms the given string to a logical path. For example, setting `path` to `A B C` produces a logical path of `/section/a-b-c`.
142
143 ## Resource map
144
145 Construct the map passed to the [`AddResource`](#addresource) method using the fields below.
146
147 Key|Description|Required
148 :--|:--|:-:
149 `content.mediaType`|The content [media type][].|:heavy_check_mark:
150 `content.value`|The content value as a string or resource.|:heavy_check_mark:
151 `name`|The resource name.|&nbsp;
152 `params`|A map of resource parameters.|&nbsp;
153 `path`|The resources's [logical path](g) relative to the content adapter. Do not include a leading slash.|:heavy_check_mark:
154 `title`|The resource title.|&nbsp;
155
156 > [!NOTE]
157 > When `content.value` is a string, Hugo generates a new resource with a publication path relative to the page. However, if `content.value` is already a resource, Hugo directly uses its value and publishes it relative to the site root. This latter method is more efficient.
158 >
159 > When setting the `path`, Hugo transforms the given string to a logical path. For example, setting `path` to `A B C/cover.jpg` produces a logical path of `/section/a-b-c/cover.jpg`.
160
161 ## Example
162
163 Create pages from remote data, where each page represents a book review.
164
165 Step 1
166 : Create the content structure.
167
168 ```tree
169 content/
170 └── books/
171 ├── _content.gotmpl <-- content adapter
172 └── _index.md
173 ```
174
175 Step 2
176 : Inspect the remote data to determine how to map key-value pairs to front matter fields.\
177 <https://gohugo.io/shared/examples/data/books.json>
178
179 Step 3
180 : Create the content adapter.
181
182 ```go-html-template {file="content/books/_content.gotmpl" copy=true}
183 {{/* Get remote data. */}}
184 {{ $data := dict }}
185 {{ $url := "https://gohugo.io/shared/examples/data/books.json" }}
186 {{ with try (resources.GetRemote $url) }}
187 {{ with .Err }}
188 {{ errorf "Unable to get remote resource %s: %s" $url . }}
189 {{ else with .Value }}
190 {{ $data = . | transform.Unmarshal }}
191 {{ else }}
192 {{ errorf "Unable to get remote resource %s" $url }}
193 {{ end }}
194 {{ end }}
195
196 {{/* Add pages and page resources. */}}
197 {{ range $data }}
198
199 {{/* Add page. */}}
200 {{ $content := dict "mediaType" "text/markdown" "value" .summary }}
201 {{ $dates := dict "date" (time.AsTime .date) }}
202 {{ $params := dict "author" .author "isbn" .isbn "rating" .rating "tags" .tags }}
203 {{ $page := dict
204 "content" $content
205 "dates" $dates
206 "kind" "page"
207 "params" $params
208 "path" .title
209 "title" .title
210 }}
211 {{ $.AddPage $page }}
212
213 {{/* Add page resource. */}}
214 {{ $item := . }}
215 {{ with $url := $item.cover }}
216 {{ with try (resources.GetRemote $url) }}
217 {{ with .Err }}
218 {{ errorf "Unable to get remote resource %s: %s" $url . }}
219 {{ else with .Value }}
220 {{ $content := dict "mediaType" .MediaType.Type "value" .Content }}
221 {{ $params := dict "alt" $item.title }}
222 {{ $resource := dict
223 "content" $content
224 "params" $params
225 "path" (printf "%s/cover.%s" $item.title .MediaType.SubType)
226 }}
227 {{ $.AddResource $resource }}
228 {{ else }}
229 {{ errorf "Unable to get remote resource %s" $url }}
230 {{ end }}
231 {{ end }}
232 {{ end }}
233
234 {{ end }}
235 ```
236
237 Step 4
238 : Create a _page_ template to render each book review.
239
240 ```go-html-template {file="layouts/books/page.html" copy=true}
241 {{ define "main" }}
242 <h1>{{ .Title }}</h1>
243
244 {{ with .Resources.GetMatch "cover.*" }}
245 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="{{ .Params.alt }}">
246 {{ end }}
247
248 <p>Author: {{ .Params.author }}</p>
249
250 <p>
251 ISBN: {{ .Params.isbn }}<br>
252 Rating: {{ .Params.rating }}<br>
253 Review date: {{ .Date | time.Format ":date_long" }}
254 </p>
255
256 {{ with .GetTerms "tags" }}
257 <p>Tags:</p>
258 <ul>
259 {{ range . }}
260 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
261 {{ end }}
262 </ul>
263 {{ end }}
264
265 {{ .Content }}
266 {{ end }}
267 ```
268
269 ## Multilingual projects
270
271 With multilingual projects you can:
272
273 1. Create one content adapter for all languages using the [`EnableAllLanguages`](#enablealllanguages) method as described above.
274 1. Create content adapters unique to each language. See the examples below.
275
276 ### Translations by file name
277
278 With this project configuration:
279
280 {{< code-toggle file=hugo >}}
281 [languages.en]
282 weight = 1
283
284 [languages.de]
285 weight = 2
286 {{< /code-toggle >}}
287
288 Include a language designator in the content adapter's file name.
289
290 ```tree
291 content/
292 └── books/
293 ├── _content.de.gotmpl
294 ├── _content.en.gotmpl
295 ├── _index.de.md
296 └── _index.en.md
297 ```
298
299 ### Translations by content directory
300
301 With this project configuration:
302
303 {{< code-toggle file=hugo >}}
304 [languages.en]
305 contentDir = 'content/en'
306 weight = 1
307
308 [languages.de]
309 contentDir = 'content/de'
310 weight = 2
311 {{< /code-toggle >}}
312
313 Create a single content adapter in each directory:
314
315 ```tree
316 content/
317 ├── de/
318 │ └── books/
319 │ ├── _content.gotmpl
320 │ └── _index.md
321 └── en/
322 └── books/
323 ├── _content.gotmpl
324 └── _index.md
325 ```
326
327 ## Page collisions
328
329 Two or more pages collide when they have the same publication path. Due to concurrency, the content of the published page is indeterminate. Consider this example:
330
331 ```tree
332 content/
333 └── books/
334 ├── _content.gotmpl <-- content adapter
335 ├── _index.md
336 └── the-hunchback-of-notre-dame.md
337 ```
338
339 If the content adapter also creates `books/the-hunchback-of-notre-dame`, the content of the published page is indeterminate. You can not define the processing order.
340
341 To detect page collisions, use the `--printPathWarnings` flag when building your project.
342
343 [content formats]: /content-management/formats/#classification
344 [examples]: /methods/page/store/
345 [front matter field]: /content-management/front-matter/#fields
346 [media type]: https://en.wikipedia.org/wiki/Media_type
347 [syntax]: /templates/introduction/
348 [template functions]: /functions/