This commit is contained in:
Bjørn Erik Pedersen 2024-06-21 09:41:24 +02:00
commit af0cb57aaf
No known key found for this signature in database
475 changed files with 7408 additions and 4720 deletions

View file

@ -15,17 +15,17 @@ The `resources.FromString` function returns a resource created from a string, ca
Hugo publishes the resource to the target path when you call its [`Publish`], [`Permalink`], or [`RelPermalink`] methods.
[`publish`]: /methods/resource/publish
[`permalink`]: /methods/resource/permalink
[`relpermalink`]: /methods/resource/relpermalink
[`publish`]: /methods/resource/publish/
[`permalink`]: /methods/resource/permalink/
[`relpermalink`]: /methods/resource/relpermalink/
Let's say you need to publish a file named "site.json" in the root of your public directory, containing the build date, the Hugo version used to build the site, and the date that the content was last modified. For example:
```json
{
"build_date": "2023-10-03T10:50:40-07:00",
"hugo_version": "0.122.0",
"last_modified": "2023-10-02T15:21:27-07:00"
"build_date": "2024-02-19T12:27:05-08:00",
"hugo_version": "0.126.0",
"last_modified": "2024-02-19T12:01:42-08:00"
}
```
@ -37,7 +37,7 @@ Place this in your baseof.html template:
{{ $m := dict
"hugo_version" hugo.Version
"build_date" (now.Format $rfc3339)
"last_modified" (site.LastChange.Format $rfc3339)
"last_modified" (site.Lastmod.Format $rfc3339)
}}
{{ $json := jsonify $m }}
{{ $r := resources.FromString "site.json" $json }}
@ -47,7 +47,7 @@ Place this in your baseof.html template:
The example above:
1. Creates a map with the relevant key/value pairs using the [`dict`] function
1. Creates a map with the relevant key-value pairs using the [`dict`] function
2. Encodes the map as a JSON string using the [`jsonify`] function
3. Creates a resource from the JSON string using the `resources.FromString` function
4. Publishes the file to the root of the public directory using the resource's `.Publish` method
@ -61,7 +61,7 @@ Combine `resources.FromString` with [`resources.ExecuteAsTemplate`] if your stri
{{ $m := dict
"hugo_version" hugo.Version
"build_date" (now.Format $rfc3339)
"last_modified" (site.LastChange.Format $rfc3339)
"last_modified" (site.Lastmod.Format $rfc3339)
}}
{{ $json := jsonify $m }}
`
@ -72,6 +72,6 @@ Combine `resources.FromString` with [`resources.ExecuteAsTemplate`] if your stri
{{ end }}
```
[`dict`]: /functions/collections/dictionary
[`jsonify`]: /functions/encoding/jsonify
[`resources.ExecuteAsTemplate`]: /functions/resources/executeastemplate
[`dict`]: /functions/collections/dictionary/
[`jsonify`]: /functions/encoding/jsonify/
[`resources.ExecuteAsTemplate`]: /functions/resources/executeastemplate/