mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
---
|
||||
title: Content adapters
|
||||
description: Create content adapters to dynamically add content when building your site.
|
||||
categories: [content management]
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: content-management
|
||||
weight: 290
|
||||
weight: 290
|
||||
toc: true
|
||||
---
|
||||
|
||||
{{< new-in 0.126.0 />}}
|
||||
@@ -39,11 +33,11 @@ Each content adapter is named _content.gotmpl and uses the same [syntax] as temp
|
||||
|
||||
Use these methods within a content adapter.
|
||||
|
||||
###### AddPage
|
||||
### AddPage
|
||||
|
||||
Adds a page to the site.
|
||||
|
||||
{{< code file=content/books/_content.gotmpl >}}
|
||||
```go-html-template {file="content/books/_content.gotmpl"}
|
||||
{{ $content := dict
|
||||
"mediaType" "text/markdown"
|
||||
"value" "The _Hunchback of Notre Dame_ was written by Victor Hugo."
|
||||
@@ -55,13 +49,13 @@ Adds a page to the site.
|
||||
"title" "The Hunchback of Notre Dame"
|
||||
}}
|
||||
{{ .AddPage $page }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
###### AddResource
|
||||
### AddResource
|
||||
|
||||
Adds a page resource to the site.
|
||||
|
||||
{{< code file=content/books/_content.gotmpl >}}
|
||||
```go-html-template {file="content/books/_content.gotmpl"}
|
||||
{{ with resources.Get "images/a.jpg" }}
|
||||
{{ $content := dict
|
||||
"mediaType" .MediaType.Type
|
||||
@@ -73,42 +67,41 @@ Adds a page resource to the site.
|
||||
}}
|
||||
{{ $.AddResource $resource }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Then retrieve the new page resource with something like:
|
||||
|
||||
{{< code file=layouts/_default/single.html >}}
|
||||
```go-html-template {file="layouts/_default/single.html"}
|
||||
{{ with .Resources.Get "cover.jpg" }}
|
||||
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
###### Site
|
||||
### Site
|
||||
|
||||
Returns the `Site` to which the pages will be added.
|
||||
|
||||
{{< code file=content/books/_content.gotmpl >}}
|
||||
```go-html-template {file="content/books/_content.gotmpl"}
|
||||
{{ .Site.Title }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Note that the `Site` returned 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".
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Note that the `Site` returned 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".
|
||||
|
||||
###### Store
|
||||
### 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](#enablealllanguages) is set. See [examples](/methods/page/store/).
|
||||
|
||||
{{< code file=content/books/_content.gotmpl >}}
|
||||
```go-html-template {file="content/books/_content.gotmpl"}
|
||||
{{ .Store.Set "key" "value" }}
|
||||
{{ .Store.Get "key" }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
###### EnableAllLanguages
|
||||
### 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.
|
||||
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 >}}
|
||||
```go-html-template {file="content/books/_content.gotmpl"}
|
||||
{{ .EnableAllLanguages }}
|
||||
{{ $content := dict
|
||||
"mediaType" "text/markdown"
|
||||
@@ -121,7 +114,7 @@ By default, Hugo executes the content adapter for the language defined by the _c
|
||||
"title" "The Hunchback of Notre Dame"
|
||||
}}
|
||||
{{ .AddPage $page }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
## Page map
|
||||
|
||||
@@ -141,11 +134,10 @@ Key|Description|Required
|
||||
`path`|The page's [logical path](g) relative to the content adapter. Do not include a leading slash or file extension.|:heavy_check_mark:
|
||||
`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 %}}
|
||||
> [!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`.
|
||||
|
||||
## Resource map
|
||||
|
||||
@@ -160,18 +152,18 @@ Key|Description|Required
|
||||
`path`|The resources's [logical path](g) relative to the content adapter. Do not include a leading slash.|:heavy_check_mark:
|
||||
`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 %}}
|
||||
> [!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`.
|
||||
|
||||
## Example
|
||||
|
||||
Create pages from remote data, where each page represents a book review.
|
||||
|
||||
Step 1
|
||||
: Create the content structure.
|
||||
### Step 1
|
||||
|
||||
Create the content structure.
|
||||
|
||||
```text
|
||||
content/
|
||||
@@ -180,15 +172,15 @@ content/
|
||||
└── _index.md
|
||||
```
|
||||
|
||||
Step 2
|
||||
: Inspect the remote data to determine how to map key-value pairs to front matter fields.
|
||||
### 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>
|
||||
|
||||
: <https://gohugo.io/shared/examples/data/books.json>
|
||||
### Step 3
|
||||
|
||||
Step 3
|
||||
: Create the content adapter.
|
||||
Create the content adapter.
|
||||
|
||||
{{< code file=content/books/_content.gotmpl copy=true >}}
|
||||
```go-html-template {file="content/books/_content.gotmpl" copy=true}
|
||||
{{/* Get remote data. */}}
|
||||
{{ $data := dict }}
|
||||
{{ $url := "https://gohugo.io/shared/examples/data/books.json" }}
|
||||
@@ -241,12 +233,13 @@ Step 3
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Step 4
|
||||
: Create a single template to render each book review.
|
||||
### Step 4
|
||||
|
||||
{{< code file=layouts/books/single.html copy=true >}}
|
||||
Create a single template to render each book review.
|
||||
|
||||
```go-html-template {file="layouts/books/single.html" copy=true}
|
||||
{{ define "main" }}
|
||||
<h1>{{ .Title }}</h1>
|
||||
|
||||
@@ -273,7 +266,7 @@ Step 4
|
||||
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
## Multilingual sites
|
||||
|
||||
|
Reference in New Issue
Block a user