fcc3ed651 Remove some expired new-in a9c5981f5 Fix cascade example 82bb250fa Add some lines about permalinks tokens in front matter 328fe564e Remove some outdated new-in fb140b153 Hide showcase menu entry 42d9d1c79 Update image formats from which EXIF data can be extracted 09ad56b6e netlify: Hugo 0.130.0 1d503f846 Merge branch 'tempv0.130.0' e2458074d math: Add trigonometric functions and some angle helper functions 392afc8f9 Disable the showcase section for now 0300750f2 Improve example of image render hook 60a9306af Improve description of the .Site.RegularPages method 8d759175d Fix typos 55daa4554 Update XxHash.md 397c81cb7 Add namespace for hash functions 70fe8d2f0 netlify: Bump Hugo 0.129.0 5a9771aff Merge branch 'tempv0.129.0' f9146575b Fix typo e6e1fea49 Fix typo in Hugo docs | functions | partial 732d10ec4 source: Expose GitInfo Body 34c97e639 netlify: Hugo 0.128.2 3270587e9 Fix typo 727c5396e netlify: Hugo 0.128.1 80b6ae99c Update GitHub Pages workflow file example 027134102 Update GitHub Pages workflow file example 2600a8a2e Miscellaneous edits 3fdd5819b Update Build.md 7764005c3 Improve example of render hook directory structure 5e3941d82 Fix typos 748bf065f Restructure templates section fafbf6566 Update Defer.md 012162e0d Document changes to template functions in v0.128.0 0990ce35b quick-reference: Update emojis 6677a30ef Update Goldmark configuration documentation 4449d530d Document new pagination config 0af8be439 Update Defer.md 56348196d netlify: Hugo 0.128.0 d67b6d82e Update content/en/functions/templates/Defer.md 23d996b3d Update content/en/functions/templates/Defer.md 7f7fb2f27 Document templates.Defer 5ada1e9d5 Fix docs merge (remove shortcode) d27ee6156 Merge branch 'tempv0.128.0' 5d7317c84 Fix typo 7c18ee546 Update theme 83bfea63b Update theme b274b3238 Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722' ff34a035a deploy: Add stripIndexHtml target option d9e964bdb markup/goldmark: Add the Hugo Goldmark Extras "delete" extension ac5bd16d2 deps: Upgrade github.com/alecthomas/chroma v2.13.0 => v2.14.0 25377171b config: Remove extraneous BuildConfig setting 0d2044f6d docs: Regen docshelper a2548dac9 markup/goldmark: Support extras extension 9d0c86ee8 commands: Add gen chromastyles --lineNumbersTableStyle flag git-subtree-dir: docs git-subtree-split: fcc3ed651a1b6431303c2f88f20fa38531c52b3d
10 KiB
title | description | categories | keywords | menu | weight | toc | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Content adapters | Create content adapters to dynamically add content when building your site. |
|
|
290 | true |
{{< new-in 0.126.0 >}}
Overview
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.
Methods
Use these methods within a content adapter.
AddPage
Adds a page to the site.
{{< code file=content/books/_content.gotmpl >}} {{ $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 }} {{< /code >}}
AddResource
Adds a page resource to the site.
{{< code file=content/books/_content.gotmpl >}} {{ 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 }} {{< /code >}}
Then retrieve the new page resource with something like:
{{< code file=layouts/_default/single.html >}} {{ with .Resources.Get "cover.jpg" }} {{ end }} {{< /code >}}
Site
Returns the Site
to which the pages will be added.
{{< code file=content/books/_content.gotmpl >}} {{ .Site.Title }} {{< /code >}}
Store
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.
{{< code file=content/books/_content.gotmpl >}} {{ .Store.Set "key" "value" }} {{ .Store.Get "key" }} {{< /code >}}
EnableAllLanguages
By default, Hugo executes the content adapter for the language defined by the _content.gotmpl file . Use this method to activate the content adapter for all languages.
{{< code file=content/books/_content.gotmpl >}} {{ .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 }} {{< /code >}}
Page map
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 | Descripion | 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. |
|
kind |
The page kind. Default is page . |
|
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. | ✔️ |
title |
The page title. |
{{% note %}}
While path
is the only required field, we recommend setting title
as well.
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
.
{{% /note %}}
Resource map
Construct the map passed to the AddResource
method using the fields below.
Key | Descripion | Required |
---|---|---|
content.mediaType |
The content media type. | ✔️ |
content.value |
The content value as a string or resource. | ✔️ |
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. | ✔️ |
title |
The resource title. |
{{% note %}}
If the content.value
is a string Hugo creates a new resource. If the content.value
is a resource, Hugo obtains the value from the existing resource.
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
.
{{% /note %}}
Example
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.
- Step 3
- Create the content adapter.
{{< code file=content/books/_content.gotmpl copy=true >}} {{/* Get remote data. */}} {{ $data := dict }} {{ $url := "https://gohugo.io/shared/examples/data/books.json" }} {{ with resources.GetRemote $url }} {{ with .Err }} {{ errorf "Unable to get remote resource %s: %s" $url . }} {{ else }} {{ $data = . | transform.Unmarshal }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %s" $url }} {{ end }}
{{/* Add pages and page resources. */}} {{ range $data }}
{{/* Add page. */}} {{ $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 }}
{{/* Add page resource. */}} {{ $item := . }} {{ with $url := $item.cover }} {{ with resources.GetRemote $url }} {{ with .Err }} {{ errorf "Unable to get remote resource %s: %s" $url . }} {{ else }} {{ $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 }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %s" $url }} {{ end }} {{ end }}
{{ end }} {{< /code >}}
- Step 4
- Create a single template to render each book review.
{{< code file=layouts/books/single.html copy=true >}} {{ define "main" }}
{{ .Title }}
{{ with .Resources.GetMatch "cover.*" }} {{ end }}
Author: {{ .Params.author }}
ISBN: {{ .Params.isbn }}
Rating: {{ .Params.rating }}
Review date: {{ .Date | time.Format ":date_long" }}
{{ with .GetTerms "tags" }}
Tags:
-
{{ range . }}
- {{ .LinkTitle }} {{ end }}
{{ .Content }} {{ end }} {{< /code >}}
Multilingual sites
With multilingual sites you can:
- Create one content adapter for all languages using the
EnableAllLanguages
method as described above. - Create content adapters unique to each language. See the examples below.
Translations by file name
With this site 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
Translations by content directory
With this site 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
Page collisions
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 site.