resources.ExecuteAsTemplate
Returns a resource created from a Go template, parsed and executed with the given context.
Syntax
resources.ExecuteAsTemplate TARGETPATH CONTEXT RESOURCE
Returns
resource.Resource
The resources.ExecuteAsTemplate function returns a resource created from a Go template, parsed and executed with the given context, 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 have a CSS file that you wish to populate with values from your project configuration:
assets/css/template.css
body {
background-color: {{ site.Params.style.bg_color }};
color: {{ site.Params.style.text_color }};
}And your project configuration contains:
params:
style:
bg_color: '#fefefe'
text_color: '#222'
[params]
[params.style]
bg_color = '#fefefe'
text_color = '#222'
{
"params": {
"style": {
"bg_color": "#fefefe",
"text_color": "#222"
}
}
}
Place this in your baseof.html template:
{{ with resources.Get "css/template.css" }}
{{ with resources.ExecuteAsTemplate "css/main.css" $ . }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}
{{ end }}The example above:
- Captures the template as a resource
- Executes the resource as a template, passing the current page in context
- Publishes the resource to css/main.css
The result is:
public/css/main.css
body {
background-color: #fefefe;
color: #222;
}Last updated:
February 25, 2026
:
Merge commit '0c2fa2460f485e0eca564dcccf36d34538374922' (b0d3364f1)
Improve this page