| 1 |
--- |
| 2 |
title: resources.FromString |
| 3 |
description: Returns a resource created from a string. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: resource.Resource |
| 10 |
signatures: [resources.FromString TARGETPATH STRING] |
| 11 |
--- |
| 12 |
|
| 13 |
The `resources.FromString` function returns a resource created from a string, caching the result using the target path as its cache key. |
| 14 |
|
| 15 |
Hugo publishes the resource to the target path when you call its [`Publish`][], [`Permalink`][], or [`RelPermalink`][] methods. |
| 16 |
|
| 17 |
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: |
| 18 |
|
| 19 |
```json |
| 20 |
{ |
| 21 |
"build_date": "2026-08-12T11:32:21-07:00", |
| 22 |
"hugo_version": "0.165.0", |
| 23 |
"last_modified": "2026-08-12T10:46:26-07:00" |
| 24 |
} |
| 25 |
``` |
| 26 |
|
| 27 |
Place this in your baseof.html template: |
| 28 |
|
| 29 |
```go-html-template |
| 30 |
{{ if .IsHome }} |
| 31 |
{{ $rfc3339 := "2006-01-02T15:04:05Z07:00" }} |
| 32 |
{{ $m := dict |
| 33 |
"hugo_version" hugo.Version |
| 34 |
"build_date" (now.Format $rfc3339) |
| 35 |
"last_modified" (site.Lastmod.Format $rfc3339) |
| 36 |
}} |
| 37 |
{{ $json := jsonify $m }} |
| 38 |
{{ $r := resources.FromString "site.json" $json }} |
| 39 |
{{ $r.Publish }} |
| 40 |
{{ end }} |
| 41 |
``` |
| 42 |
|
| 43 |
The example above: |
| 44 |
|
| 45 |
1. Creates a map with the relevant key-value pairs using the [`dict`][] function |
| 46 |
1. Encodes the map as a JSON string using the [`jsonify`][] function |
| 47 |
1. Creates a resource from the JSON string using the `resources.FromString` function |
| 48 |
1. Publishes the file to the root of the `public` directory using the resource's `Publish` method |
| 49 |
|
| 50 |
Combine `resources.FromString` with [`resources.ExecuteAsTemplate`][] if your string contains template actions. Rewriting the example above: |
| 51 |
|
| 52 |
```go-html-template |
| 53 |
{{ if .IsHome }} |
| 54 |
{{ $string := ` |
| 55 |
{{ $rfc3339 := "2006-01-02T15:04:05Z07:00" }} |
| 56 |
{{ $m := dict |
| 57 |
"hugo_version" hugo.Version |
| 58 |
"build_date" (now.Format $rfc3339) |
| 59 |
"last_modified" (site.Lastmod.Format $rfc3339) |
| 60 |
}} |
| 61 |
{{ $json := jsonify $m }} |
| 62 |
` |
| 63 |
}} |
| 64 |
{{ $r := resources.FromString "" $string }} |
| 65 |
{{ $r = $r | resources.ExecuteAsTemplate "site.json" . }} |
| 66 |
{{ $r.Publish }} |
| 67 |
{{ end }} |
| 68 |
``` |
| 69 |
|
| 70 |
[`Permalink`]: /methods/resource/permalink/ |
| 71 |
[`Publish`]: /methods/resource/publish/ |
| 72 |
[`RelPermalink`]: /methods/resource/relpermalink/ |
| 73 |
[`dict`]: /functions/collections/dictionary/ |
| 74 |
[`jsonify`]: /functions/encoding/jsonify/ |
| 75 |
[`resources.ExecuteAsTemplate`]: /functions/resources/executeastemplate/ |