hugo/docs/content/en/hugo-pipes/resource-from-template.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

1 KiB
Executable file

title linkTitle description categories keywords menu weight action
ExecuteAsTemplate Resource from template Creates a resource from a template
asset management
docs
parent weight
hugo-pipes 120
120
aliases returnType signatures
resource.Resource
resources.ExecuteAsTemplate TARGETPATH CONTEXT RESOURCE

Usage

In order to use Hugo Pipes function on an asset file containing Go Template magic the function resources.ExecuteAsTemplate must be used.

The function takes three arguments: the target path for the created resource, the template context, and the resource object. The target path is used to cache the result.

// assets/sass/template.scss
$backgroundColor: {{ .Param "backgroundColor" }};
$textColor: {{ .Param "textColor" }};
body{
  background-color:$backgroundColor;
  color: $textColor;
}
// [...]
{{ $sassTemplate := resources.Get "sass/template.scss" }}
{{ $style := $sassTemplate | resources.ExecuteAsTemplate "main.scss" . | resources.ToCSS }}