diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-06-05 12:44:45 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-06-06 13:32:12 +0200 |
commit | fcd63de3a54fadcd30972654d8eb86dc4d889784 (patch) | |
tree | 5140863493b65783f73ecab8885f684fc623b1da /docs | |
parent | 150d75738b54acddc485d363436757189144da6a (diff) |
tpl/data: Misc header improvements, tests, allow multiple headers of same key
Closes #5617
Diffstat (limited to 'docs')
-rw-r--r-- | docs/content/en/templates/data-templates.md | 34 |
1 files changed, 12 insertions, 22 deletions
diff --git a/docs/content/en/templates/data-templates.md b/docs/content/en/templates/data-templates.md index 661c0bdfa..b3edf8f44 100644 --- a/docs/content/en/templates/data-templates.md +++ b/docs/content/en/templates/data-templates.md @@ -114,19 +114,10 @@ You can use the following code to render the `Short Description` in your layout: Note the use of the [`markdownify` template function][markdownify]. This will send the description through the Blackfriday Markdown rendering engine. -<!-- begin "Data-drive Content" page --> -## Data-Driven Content +## Get Remote Data -In addition to the [data files](/extras/datafiles/) feature, Hugo also has a "data-driven content" feature, which lets you load any [JSON](https://www.json.org/) or [CSV](https://en.wikipedia.org/wiki/Comma-separated_values) file from nearly any resource. - -Data-driven content currently consists of two functions, `getJSON` and `getCSV`, which are available in all template files. - -## Implementation details - -### Call the Functions with a URL - -In your template, call the functions like this: +Use `getJSON` or `getCSV` to get remote data: ``` {{ $dataJ := getJSON "url" }} @@ -155,19 +146,18 @@ This will resolve internally to the following: {{ $gistJ := getJSON "https://api.github.com/users/GITHUB_USERNAME/gists" }} ``` -Finally, you can range over an array. This example will output the -first 5 gists for a GitHub user: +### Add HTTP headers + +{{< new-in "0.84.0" >}} Both `getJSON` and `getCSV` takes an optional map as the last argument, e.g.: ``` -<ul> - {{ $urlPre := "https://api.github.com" }} - {{ $gistJ := getJSON $urlPre "/users/GITHUB_USERNAME/gists" }} - {{ range first 5 $gistJ }} - {{ if .public }} - <li><a href="{{ .html_url }}" target="_blank">{{ .description }}</a></li> - {{ end }} - {{ end }} -</ul> +{{ $data := getJSON "https://example.org/api" (dict "Authorization" "Bearer abcd") }} +``` + +If you need multiple values for the same header key, use a slice: + +``` +{{ $data := getJSON "https://example.org/api" (dict "X-List" (slice "a" "b" "c")) }} ``` ### Example for CSV files |