| 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, with these exceptions: |
| 123 |
|
| 124 |
- Instead of setting the `markup` field, specify the `content.mediaType` as described below. |
| 125 |
- Instead of setting taxonomy keys such as `tags` and `categories` at the top level, place them inside the `params` map. |
| 126 |
|
| 127 |
This table describes the fields most commonly passed to the `AddPage` method. |
| 128 |
|
| 129 |
Key|Description|Required |
| 130 |
:--|:--|:-: |
| 131 |
`content.mediaType`|The content [media type][]. Default is `text/markdown`. See [content formats][] for examples.| |
| 132 |
`content.value`|The content value as a string.| |
| 133 |
`dates.date`|The page creation date as a `time.Time` value.| |
| 134 |
`dates.expiryDate`|The page expiry date as a `time.Time` value.| |
| 135 |
`dates.lastmod`|The page last modification date as a `time.Time` value.| |
| 136 |
`dates.publishDate`|The page publication date as a `time.Time` value.| |
| 137 |
`params`|A map of page parameters.| |
| 138 |
`path`|The page's [logical path](g) relative to the content adapter. Do not include a leading slash or file extension.|:heavy_check_mark: |
| 139 |
`title`|The page title.| |
| 140 |
|
| 141 |
> [!NOTE] |
| 142 |
> While `path` is the only required field, we recommend setting `title` as well. |
| 143 |
> |
| 144 |
> 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`. |
| 145 |
|
| 146 |
## Resource map |
| 147 |
|
| 148 |
Construct the map passed to the [`AddResource`](#addresource) method using the fields below. |
| 149 |
|
| 150 |
Key|Description|Required |
| 151 |
:--|:--|:-: |
| 152 |
`content.mediaType`|The content [media type][].|:heavy_check_mark: |
| 153 |
`content.value`|The content value as a string or resource.|:heavy_check_mark: |
| 154 |
`name`|The resource name.| |
| 155 |
`params`|A map of resource parameters.| |
| 156 |
`path`|The resources's [logical path](g) relative to the content adapter. Do not include a leading slash.|:heavy_check_mark: |
| 157 |
`title`|The resource title.| |
| 158 |
|
| 159 |
> [!NOTE] |
| 160 |
> 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. |
| 161 |
> |
| 162 |
> 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`. |
| 163 |
|
| 164 |
## Example |
| 165 |
|
| 166 |
Create pages from remote data, where each page represents a book review. |
| 167 |
|
| 168 |
Step 1 |
| 169 |
: Create the content structure. |
| 170 |
|
| 171 |
```tree |
| 172 |
content/ |
| 173 |
└── books/ |
| 174 |
├── _content.gotmpl <-- content adapter |
| 175 |
└── _index.md |
| 176 |
``` |
| 177 |
|
| 178 |
Step 2 |
| 179 |
: Inspect the remote data to determine how to map key-value pairs to front matter fields.\ |
| 180 |
<https://gohugo.io/shared/examples/data/books.json> |
| 181 |
|
| 182 |
Step 3 |
| 183 |
: Create the content adapter. |
| 184 |
|
| 185 |
```go-html-template {file="content/books/_content.gotmpl" copy=true} |
| 186 |
{{/* Get remote data. */}} |
| 187 |
{{ $data := dict }} |
| 188 |
{{ $url := "https://gohugo.io/shared/examples/data/books.json" }} |
| 189 |
{{ with try (resources.GetRemote $url) }} |
| 190 |
{{ with .Err }} |
| 191 |
{{ errorf "Unable to get remote resource %s: %s" $url . }} |
| 192 |
{{ else with .Value }} |
| 193 |
{{ $data = . | transform.Unmarshal }} |
| 194 |
{{ else }} |
| 195 |
{{ errorf "Unable to get remote resource %s" $url }} |
| 196 |
{{ end }} |
| 197 |
{{ end }} |
| 198 |
|
| 199 |
{{/* Add pages and page resources. */}} |
| 200 |
{{ range $data }} |
| 201 |
|
| 202 |
{{/* Add page. */}} |
| 203 |
{{ $content := dict "mediaType" "text/markdown" "value" .summary }} |
| 204 |
{{ $dates := dict "date" (time.AsTime .date) }} |
| 205 |
{{ $params := dict "author" .author "isbn" .isbn "rating" .rating "tags" .tags }} |
| 206 |
{{ $page := dict |
| 207 |
"content" $content |
| 208 |
"dates" $dates |
| 209 |
"kind" "page" |
| 210 |
"params" $params |
| 211 |
"path" .title |
| 212 |
"title" .title |
| 213 |
}} |
| 214 |
{{ $.AddPage $page }} |
| 215 |
|
| 216 |
{{/* Add page resource. */}} |
| 217 |
{{ $item := . }} |
| 218 |
{{ with $url := $item.cover }} |
| 219 |
{{ with try (resources.GetRemote $url) }} |
| 220 |
{{ with .Err }} |
| 221 |
{{ errorf "Unable to get remote resource %s: %s" $url . }} |
| 222 |
{{ else with .Value }} |
| 223 |
{{ $content := dict "mediaType" .MediaType.Type "value" .Content }} |
| 224 |
{{ $params := dict "alt" $item.title }} |
| 225 |
{{ $resource := dict |
| 226 |
"content" $content |
| 227 |
"params" $params |
| 228 |
"path" (printf "%s/cover.%s" $item.title .MediaType.SubType) |
| 229 |
}} |
| 230 |
{{ $.AddResource $resource }} |
| 231 |
{{ else }} |
| 232 |
{{ errorf "Unable to get remote resource %s" $url }} |
| 233 |
{{ end }} |
| 234 |
{{ end }} |
| 235 |
{{ end }} |
| 236 |
|
| 237 |
{{ end }} |
| 238 |
``` |
| 239 |
|
| 240 |
Step 4 |
| 241 |
: Create a _page_ template to render each book review. |
| 242 |
|
| 243 |
```go-html-template {file="layouts/books/page.html" copy=true} |
| 244 |
{{ define "main" }} |
| 245 |
<h1>{{ .Title }}</h1> |
| 246 |
|
| 247 |
{{ with .Resources.GetMatch "cover.*" }} |
| 248 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="{{ .Params.alt }}"> |
| 249 |
{{ end }} |
| 250 |
|
| 251 |
<p>Author: {{ .Params.author }}</p> |
| 252 |
|
| 253 |
<p> |
| 254 |
ISBN: {{ .Params.isbn }}<br> |
| 255 |
Rating: {{ .Params.rating }}<br> |
| 256 |
Review date: {{ .Date | time.Format ":date_long" }} |
| 257 |
</p> |
| 258 |
|
| 259 |
{{ with .GetTerms "tags" }} |
| 260 |
<p>Tags:</p> |
| 261 |
<ul> |
| 262 |
{{ range . }} |
| 263 |
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li> |
| 264 |
{{ end }} |
| 265 |
</ul> |
| 266 |
{{ end }} |
| 267 |
|
| 268 |
{{ .Content }} |
| 269 |
{{ end }} |
| 270 |
``` |
| 271 |
|
| 272 |
## Multilingual projects |
| 273 |
|
| 274 |
With multilingual projects you can: |
| 275 |
|
| 276 |
1. Create one content adapter for all languages using the [`EnableAllLanguages`](#enablealllanguages) method as described above. |
| 277 |
1. Create content adapters unique to each language. See the examples below. |
| 278 |
|
| 279 |
### Translations by file name |
| 280 |
|
| 281 |
With this project configuration: |
| 282 |
|
| 283 |
{{< code-toggle file=hugo >}} |
| 284 |
[languages.en] |
| 285 |
weight = 1 |
| 286 |
|
| 287 |
[languages.de] |
| 288 |
weight = 2 |
| 289 |
{{< /code-toggle >}} |
| 290 |
|
| 291 |
Include a language designator in the content adapter's file name. |
| 292 |
|
| 293 |
```tree |
| 294 |
content/ |
| 295 |
└── books/ |
| 296 |
├── _content.de.gotmpl |
| 297 |
├── _content.en.gotmpl |
| 298 |
├── _index.de.md |
| 299 |
└── _index.en.md |
| 300 |
``` |
| 301 |
|
| 302 |
### Translations by content directory |
| 303 |
|
| 304 |
With this project configuration: |
| 305 |
|
| 306 |
{{< code-toggle file=hugo >}} |
| 307 |
[languages.en] |
| 308 |
contentDir = 'content/en' |
| 309 |
weight = 1 |
| 310 |
|
| 311 |
[languages.de] |
| 312 |
contentDir = 'content/de' |
| 313 |
weight = 2 |
| 314 |
{{< /code-toggle >}} |
| 315 |
|
| 316 |
Create a single content adapter in each directory: |
| 317 |
|
| 318 |
```tree |
| 319 |
content/ |
| 320 |
├── de/ |
| 321 |
│ └── books/ |
| 322 |
│ ├── _content.gotmpl |
| 323 |
│ └── _index.md |
| 324 |
└── en/ |
| 325 |
└── books/ |
| 326 |
├── _content.gotmpl |
| 327 |
└── _index.md |
| 328 |
``` |
| 329 |
|
| 330 |
## Page collisions |
| 331 |
|
| 332 |
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: |
| 333 |
|
| 334 |
```tree |
| 335 |
content/ |
| 336 |
└── books/ |
| 337 |
├── _content.gotmpl <-- content adapter |
| 338 |
├── _index.md |
| 339 |
└── the-hunchback-of-notre-dame.md |
| 340 |
``` |
| 341 |
|
| 342 |
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. |
| 343 |
|
| 344 |
To detect page collisions, use the `--printPathWarnings` flag when building your project. |
| 345 |
|
| 346 |
[content formats]: /content-management/formats/#classification |
| 347 |
[examples]: /methods/page/store/ |
| 348 |
[front matter field]: /content-management/front-matter/#fields |
| 349 |
[media type]: https://en.wikipedia.org/wiki/Media_type |
| 350 |
[syntax]: /templates/introduction/ |
| 351 |
[template functions]: /functions/ |