hugo/docs/content/en/hugo-pipes/resource-from-string.md
2023-05-22 16:47:07 +02:00

949 B
Executable file

title linkTitle description categories keywords menu weight signature
FromString Resource from String Creates a resource from a string.
asset management
docs
parent weight
pipes 90
90
resources.FromString TARGET_PATH CONTENT

Usage

It is possible to create a resource directly from the template using resources.FromString which takes two arguments, the target path for the created resource and the given content string.

The following example creates a resource file containing localized variables for every project's languages.

{{ $string := (printf "var rootURL = '%s'; var apiURL = '%s';" (absURL "/") (.Param "API_URL")) }}
{{ $targetPath := "js/vars.js" }}
{{ $vars := $string | resources.FromString $targetPath }}
{{ $global := resources.Get "js/global.js" | resources.Minify }}

<script src="{{ $vars.Permalink }}"></script>
<script src="{{ $global.Permalink }}"></script>