tpl/data: Misc header improvements, tests, allow multiple headers of same key

Closes #5617
This commit is contained in:
Bjørn Erik Pedersen 2021-06-05 12:44:45 +02:00
parent e4560893c9
commit e1875742e2

View File

@ -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. 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. Use `getJSON` or `getCSV` to get remote data:
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:
``` ```
{{ $dataJ := getJSON "url" }} {{ $dataJ := getJSON "url" }}
@ -155,19 +146,18 @@ This will resolve internally to the following:
{{ $gistJ := getJSON "https://api.github.com/users/GITHUB_USERNAME/gists" }} {{ $gistJ := getJSON "https://api.github.com/users/GITHUB_USERNAME/gists" }}
``` ```
Finally, you can range over an array. This example will output the ### Add HTTP headers
first 5 gists for a GitHub user:
{{< new-in "0.84.0" >}} Both `getJSON` and `getCSV` takes an optional map as the last argument, e.g.:
``` ```
<ul> {{ $data := getJSON "https://example.org/api" (dict "Authorization" "Bearer abcd") }}
{{ $urlPre := "https://api.github.com" }} ```
{{ $gistJ := getJSON $urlPre "/users/GITHUB_USERNAME/gists" }}
{{ range first 5 $gistJ }} If you need multiple values for the same header key, use a slice:
{{ if .public }}
<li><a href="{{ .html_url }}" target="_blank">{{ .description }}</a></li> ```
{{ end }} {{ $data := getJSON "https://example.org/api" (dict "X-List" (slice "a" "b" "c")) }}
{{ end }}
</ul>
``` ```
### Example for CSV files ### Example for CSV files