Files
hugo/content/en/functions/resources/FromString.md
T
Bjørn Erik Pedersen 08e1ea5c70 Squashed 'docs/' changes from 71f739460..1ad3c75ad
1ad3c75ad theme: Run ncu -u to update dependencies (#3311)
2942d3753 content: Update version refs
e298fdfaf content: Remove ref to old version
c1fc876ea content: Improve taxonomic weight example
d706a013d content: Document additional functions
5751c37cc content: Standardize string function signatures
d65e6fae7 misc: Add data/docs.yaml to ignorePaths in cspell config
ba1f98a32 content: Improve collections.First/Last string examples
d7b5a2efd content: Improve collections.First/Last examples
e94e49db2 content: Add Smart Hugo to editor plugins
af204597a content: Clarify config description
9d1cb33cd content: Fix typo
dded23962 content: Clarify note on rare use case for PageRef method
8af9e3be4 content: Update the Homebrew installation description
be56390b3 content: Clarify command description
fa228e9f0 content: Clarify AsciiDoc extensions setting description
3a23a4af2 docs: Add best practices for manual summary marker placement
050d7c75e misc: Fix cspell configuration
74642b18d content: Miscellaneous edits
75a2c8425 content: Add missing reference definitions
6303f27e1 content: Improve description of the SITE.Home method
4dfbd647f Revert the minimal analytics changes (#3265)
c369e534d theme: Move tracking to turbo:load
88b451022 theme: Move tracking to turbo:render
7040dc5ee Move to minimalanalytics for GA tracking
4e8a523fb content: Update KaTeX link attributes to v0.16.25 (phase 2)
21046b942 content: Update KaTeX link attributes to v0.16.25 (phase 1)
b8df9d941 theme: Use Path for the glossary link destination
acd6c2954 theme: Use page Path when generating Markdown links
543f78340 content: Fix formatting
aef89d192 content: Correct page collection quick-reference data
08645236a content: Use RSS output format in shortcode lookup example
58654c106 content: Fix archetype lookup order
9f79bacc4 content: Describe valid uses of the nil keyword
41f139d14 theme: Remove the root view transition
6ab407bb9 content: Improve links on lang.Translate page
91e724065 content: Add missing fragments to links in the module mounts description
2f6b4a58c content: Clarify ByCount sort order
8594590b2 Update netlify.toml
811574060 content: Update version references
a12bcf7dd content: Update version references
23ca199ed Update netlify.toml
5d3da2351 content: Update version references
1617ee75b misc: Update Netlify config to build with v0.152.0
6467bda93 content: Remove outdated new-in badges
4b3ef4bd5 content: Improve description of default mount behavior
5a861f500 content: Revise partials.IncludeCached description and signature
2ad9d705a content: Correct the partials.IncludeCached signature
9f611b879 Update netlify.toml
ca21f5fbd content: Add warning about automatic summaries
2065fadde content: Update commit message guidelines
a0784f261 content: Remove outdated new-in badges
ce5a249f2 content: Remove Codeberg Pages documentation
3424b95f2 content: Update CLI docs
27fd350a6 content: Change KaTeX version references to 0.16.23
ba8c5fd3b content: Change MathJax version reference from v3 to v4
c83465222 content: Fix broken link
c6cde1074 content: Update version references
467277939 content: Fix typo
e424b6b19 content: Document new features in v0.151.0
a46898f79 Update netlify.toml
f8407c3b5 content: Enhance content view template details
974175690 content: Optionally disable whitespace trimming in fenced code blocks
25bffc4c4 Update netlify.toml
98e9ef5f0 theme: Move initColorScheme() to head
a1566495b theme: Preload fonts (#3209)
c0271d266 content: Add missing word in deploy-with-rsync.md
47099c4fa content: Remove outdated new-in badges
439b5c18b content: Fix collections.D seed example
d2486ba67 content: Update description of IsNode method
4de8815e9 content: Update description of the cascade front matter field
0801e9b3f content: Fix broken link
9bbbfb59d content: Update Netlify hosting guide and version refs
4f444fb53 misc: Update docs.yaml
a7a9a7563 Update module.md
a7909fe37 Update netlify.toml
47a2c2038 content: Update collections.D page to remove casting note
ecca5ef48 content: Update summaryLength description with default value
310e11195 content: Change alias example
bd6b53b5d content: Update minify config documentation
21bb0bb66 Update netlify.toml
68912f1a3 content: Update version reference
28a47a50f content: Fix missing link to latest release under Prebuilt Binaries
8eac2a120 theme: Revise inline code span styling
2e71b6d0a Adjust sponsor
d93340a78 content: Update documentation guide
9f6213ebe content: Fix glob pattern-alternatives syntax in cascade.md
1f61fd6be content: Add seed examples to collections.D
65385a5d0 content: Fix Cloudflare deploy not_found_handling value
4f70df235 content: Fix typo
fc216f2d9 content: Fix broken link
e98b340f2 Update cascade.md
e316dbf93 content: Update version references
f8e863e97 misc: Remove textlint config file
3b9c3e2a1 content: Miscellaneous updates related to v0.149.0
3ccc439fc Update netlify.toml
07f5140e8 Merge branch 'tempv0.149.0'
17f8d33d8 content: Add format option to transform.Unmarshal
a6a971596 content: Document the collections.D function
66eee712b content: Update front matter configuration example
863945e13 content: Update fmt.Println example
1d9a92bc7 theme: Don't exclude current section from related content
1386d8ed2 Update introduction.md
0ac89b015 content: Replace inline URLs with autolinks
54fad1e01 content: Fix typo
e09f6b0c5 content: Wrap calls to eturl shortcode in angle brackets
23efd9766 Merge commit 'bfa74537929f409fca841540b971125b7678963a'
e98754e72 resources/page: Add :sectionslug and :sectionslugs permalink tokens

git-subtree-dir: docs
git-subtree-split: 1ad3c75ad0e997d66598c1453db9989fde7d9680
2025-12-19 10:04:19 +01:00

2.4 KiB

title, description, categories, keywords, params
title description categories keywords params
resources.FromString Returns a resource created from a string.
functions_and_methods
aliases returnType signatures
resource.Resource
resources.FromString TARGETPATH STRING

The resources.FromString function returns a resource created from a string, caching the result using the target path as its cache key.

Hugo publishes the resource to the target path when you call its Publish, Permalink, or RelPermalink methods.

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:

{
  "build_date": "2025-07-08T13:12:19-07:00",
  "hugo_version": "0.152.2",
  "last_modified": "2025-07-07T22:09:13-07:00"
}

Place this in your baseof.html template:

{{ if .IsHome }}
  {{ $rfc3339 := "2006-01-02T15:04:05Z07:00" }}
  {{ $m := dict
    "hugo_version" hugo.Version
    "build_date" (now.Format $rfc3339)
    "last_modified" (site.Lastmod.Format $rfc3339)
  }}
  {{ $json := jsonify $m }}
  {{ $r := resources.FromString "site.json" $json }}
  {{ $r.Publish }}
{{ end }}

The example above:

  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

Combine resources.FromString with resources.ExecuteAsTemplate if your string contains template actions. Rewriting the example above:

{{ if .IsHome }}
  {{ $string := `
    {{ $rfc3339 := "2006-01-02T15:04:05Z07:00" }}
    {{ $m := dict
      "hugo_version" hugo.Version
      "build_date" (now.Format $rfc3339)
      "last_modified" (site.Lastmod.Format $rfc3339)
    }}
    {{ $json := jsonify $m }}
    `
  }}
  {{ $r := resources.FromString "" $string }}
  {{ $r = $r | resources.ExecuteAsTemplate "site.json" . }}
  {{ $r.Publish }}
{{ end }}