docs/content/en/content-management/content-adapters.md
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.
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 will be relative to the content adapter.
content/
├── articles/
│ ├── _index.md
│ ├── article-1.md
│ └── article-2.md
├── books/
│ ├── _content.gotmpl <-- content adapter
│ └── _index.md
└── films/
├── _content.gotmpl <-- content adapter
└── _index.md
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.
Use these methods within a content adapter.
Adds a page to the site.
{{ $content := dict
"mediaType" "text/markdown"
"value" "The _Hunchback of Notre Dame_ was written by Victor Hugo."
}}
{{ $page := dict
"content" $content
"kind" "page"
"path" "the-hunchback-of-notre-dame"
"title" "The Hunchback of Notre Dame"
}}
{{ .AddPage $page }}
Adds a page resource to the site.
{{ with resources.Get "images/a.jpg" }}
{{ $content := dict
"mediaType" .MediaType.Type
"value" .
}}
{{ $resource := dict
"content" $content
"path" "the-hunchback-of-notre-dame/cover.jpg"
}}
{{ $.AddResource $resource }}
{{ end }}
Then retrieve the new page resource with something like:
{{ with .Resources.Get "cover.jpg" }}
{{ end }}
Returns the Site to which the pages will be added.
{{ .Site.Title }}
[!note] Note that the
Sitereturned isn't fully built when invoked from the content adapters; if you try to call methods that depends on pages, e.g..Site.Pages, you will get an error saying "this method cannot be called before the site is fully initialized".
Returns a persistent "scratch pad" to store and manipulate data. The main use case for this is to transfer values between executions when EnableAllLanguages is set. See examples.
{{ .Store.Set "key" "value" }}
{{ .Store.Get "key" }}
By default, Hugo executes the content adapter only once for the first matching site in the sites matrix. Use this method to expand execution to all languages while maintaining the current role and version.
For more fine-grained control, define a sites.matrix in front matter or in a content mount.
{{ .EnableAllLanguages }}
{{ $content := dict
"mediaType" "text/markdown"
"value" "The _Hunchback of Notre Dame_ was written by Victor Hugo."
}}
{{ $page := dict
"content" $content
"kind" "page"
"path" "the-hunchback-of-notre-dame"
"title" "The Hunchback of Notre Dame"
}}
{{ .AddPage $page }}
By default, Hugo executes the content adapter only once for the first matching site in the sites matrix. Use this method to expand execution to every possible combination of language, version, and role.
For more fine-grained control, define a sites.matrix in front matter or in a content mount.
{{< new-in v0.153.0 />}}
Set any front matter field in the map passed to the AddPage method, excluding markup. Instead of setting the markup field, specify the content.mediaType as described below.
This table describes the fields most commonly passed to the AddPage method.
| Key | Description | Required |
|---|---|---|
content.mediaType | The content media type. Default is text/markdown. See content formats for examples. | |
content.value | The content value as a string. | |
dates.date | The page creation date as a time.Time value. | |
dates.expiryDate | The page expiry date as a time.Time value. | |
dates.lastmod | The page last modification date as a time.Time value. | |
dates.publishDate | The page publication date as a time.Time value. | |
params | A map of page parameters. | |
path | The page's logical path relative to the content adapter. Do not include a leading slash or file extension. | :heavy_check_mark: |
title | The page title. |
[!note] While
pathis the only required field, we recommend settingtitleas well.When setting the
path, Hugo transforms the given string to a logical path. For example, settingpathtoA B Cproduces a logical path of/section/a-b-c.
Construct the map passed to the AddResource method using the fields below.
| Key | Description | Required |
|---|---|---|
content.mediaType | The content media type. | :heavy_check_mark: |
content.value | The content value as a string or resource. | :heavy_check_mark: |
name | The resource name. | |
params | A map of resource parameters. | |
path | The resources's logical path relative to the content adapter. Do not include a leading slash. | :heavy_check_mark: |
title | The resource title. |
[!note] When
content.valueis a string, Hugo generates a new resource with a publication path relative to the page. However, ifcontent.valueis already a resource, Hugo directly uses its value and publishes it relative to the site root. This latter method is more efficient.When setting the
path, Hugo transforms the given string to a logical path. For example, settingpathtoA B C/cover.jpgproduces a logical path of/section/a-b-c/cover.jpg.
Create pages from remote data, where each page represents a book review.
Step 1 : Create the content structure.
content/
└── books/
├── _content.gotmpl <-- content adapter
└── _index.md
Step 2
: Inspect the remote data to determine how to map key-value pairs to front matter fields.
https://gohugo.io/shared/examples/data/books.json
Step 3 : Create the content adapter.
{}
{{ $data := dict }}
{{ $url := "https://gohugo.io/shared/examples/data/books.json" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "Unable to get remote resource %s: %s" $url . }}
{{ else with .Value }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get remote resource %s" $url }}
{{ end }}
{{ end }}
{}
{{ range $data }}
{}
{{ $content := dict "mediaType" "text/markdown" "value" .summary }}
{{ $dates := dict "date" (time.AsTime .date) }}
{{ $params := dict "author" .author "isbn" .isbn "rating" .rating "tags" .tags }}
{{ $page := dict
"content" $content
"dates" $dates
"kind" "page"
"params" $params
"path" .title
"title" .title
}}
{{ $.AddPage $page }}
{}
{{ $item := . }}
{{ with $url := $item.cover }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "Unable to get remote resource %s: %s" $url . }}
{{ else with .Value }}
{{ $content := dict "mediaType" .MediaType.Type "value" .Content }}
{{ $params := dict "alt" $item.title }}
{{ $resource := dict
"content" $content
"params" $params
"path" (printf "%s/cover.%s" $item.title .MediaType.SubType)
}}
{{ $.AddResource $resource }}
{{ else }}
{{ errorf "Unable to get remote resource %s" $url }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
Step 4 : Create a page template to render each book review.
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ with .Resources.GetMatch "cover.*" }}
{{ end }}
<p>Author: {{ .Params.author }}</p>
<p>
ISBN: {{ .Params.isbn }}
Rating: {{ .Params.rating }}
Review date: {{ .Date | time.Format ":date_long" }}
</p>
{{ with .GetTerms "tags" }}
<p>Tags:</p>
<ul>
{{ range . }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}
{{ .Content }}
{{ end }}
With multilingual projects you can:
EnableAllLanguages method as described above.With this project configuration:
{{< code-toggle file=hugo >}} [languages.en] weight = 1
[languages.de] weight = 2 {{< /code-toggle >}}
Include a language designator in the content adapter's file name.
content/
└── books/
├── _content.de.gotmpl
├── _content.en.gotmpl
├── _index.de.md
└── _index.en.md
With this project configuration:
{{< code-toggle file=hugo >}} [languages.en] contentDir = 'content/en' weight = 1
[languages.de] contentDir = 'content/de' weight = 2 {{< /code-toggle >}}
Create a single content adapter in each directory:
content/
├── de/
│ └── books/
│ ├── _content.gotmpl
│ └── _index.md
└── en/
└── books/
├── _content.gotmpl
└── _index.md
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:
content/
└── books/
├── _content.gotmpl <-- content adapter
├── _index.md
└── the-hunchback-of-notre-dame.md
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.
To detect page collisions, use the --printPathWarnings flag when building your project.