| 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 |
For example, to create and publish a `security.txt` file from your site configuration: |
| 16 |
|
| 17 |
```go-html-template {file="layouts/baseof.html"} |
| 18 |
{{ $content := printf "Contact: mailto:%s\n" site.Params.email }} |
| 19 |
{{ with resources.FromString ".well-known/security.txt" $content }} |
| 20 |
{{ .Publish }} |
| 21 |
{{ end }} |
| 22 |
``` |
| 23 |
|
| 24 |
To publish within a pipeline, use the [`resources.Publish`][] function: |
| 25 |
|
| 26 |
```go-html-template {file="layouts/baseof.html"} |
| 27 |
{{ $content := printf "Contact: mailto:%s\n" site.Params.email }} |
| 28 |
{{ resources.FromString ".well-known/security.txt" $content | resources.Publish }} |
| 29 |
``` |
| 30 |
|
| 31 |
The [`Permalink`][] and [`RelPermalink`][] methods also publish the resource. |
| 32 |
|
| 33 |
Combine `resources.FromString` with [`resources.ExecuteAsTemplate`][] if your string contains template actions: |
| 34 |
|
| 35 |
```go-html-template {file="layouts/baseof.html"} |
| 36 |
{{ $string := `Contact: mailto:{{ site.Params.email }} |
| 37 |
Expires: {{ (now.AddDate 1 0 0).UTC.Format "2006-01-02T15:04:05Z" }} |
| 38 |
` }} |
| 39 |
{{ $r := resources.FromString "" $string }} |
| 40 |
{{ $r = $r | resources.ExecuteAsTemplate ".well-known/security.txt" . }} |
| 41 |
{{ $r.Publish }} |
| 42 |
``` |
| 43 |
|
| 44 |
[`Permalink`]: /methods/resource/permalink/ |
| 45 |
[`RelPermalink`]: /methods/resource/relpermalink/ |
| 46 |
[`resources.ExecuteAsTemplate`]: /functions/resources/executeastemplate/ |
| 47 |
[`resources.Publish`]: /functions/resources/publish/ |