gohugoio/hugo · error
failed to parse Resource %q as Template:: %w
Error message
failed to parse Resource %q as Template:: %w
What it means
resources.ExecuteAsTemplate parses the source resource's content as a Hugo (Go text) template before executing it with the provided data. This error wraps the parse failure: the resource's content contains invalid template syntax, so it cannot be compiled. The resource path is included so you can find the offending file.
Source
Thrown at resources/resource_transformers/templates/execute_as_template.go:62
}
type executeAsTemplateTransform struct {
rs *resources.Spec
t tplimpl.TemplateStoreProvider
targetPath string
data any
}
func (t *executeAsTemplateTransform) Key() internal.ResourceTransformationKey {
return internal.NewResourceTransformationKey("execute-as-template", t.targetPath)
}
func (t *executeAsTemplateTransform) Transform(ctx *resources.ResourceTransformationCtx) error {
tplStr := helpers.ReaderToString(ctx.From)
th := t.t.GetTemplateStore()
ti, err := th.TextParse(ctx.InPath, tplStr)
if err != nil {
return fmt.Errorf("failed to parse Resource %q as Template:: %w", ctx.InPath, err)
}
ctx.OutPath = t.targetPath
return th.ExecuteWithContext(ctx.Ctx, ti, ctx.To, t.data)
}
func (c *Client) ExecuteAsTemplate(ctx context.Context, res resources.ResourceTransformer, targetPath string, data any) (resource.Resource, error) {
return res.TransformWithContext(ctx, &executeAsTemplateTransform{
rs: c.rs,
targetPath: paths.ToSlashTrimLeading(targetPath),
t: c.t,
data: data,
})
}
View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Open the named resource and fix the Go template syntax error reported in the wrapped message (line/column are included).
- If the file contains literal double braces not meant for Go templates, escape them: {{ "{{" }} ... or wrap the section differently.
- If the file needn't be templated at all, use resources.Get / .Content directly instead of ExecuteAsTemplate.
Example fix
<!-- before: assets/js/config.js contains `const t = `${x}` and {{ .Site.Title }` (unclosed) -->
{{ $r := resources.Get "js/config.js" | resources.ExecuteAsTemplate "js/config.js" . }}
<!-- after: fix syntax in the asset -->
const title = {{ .Site.Title | jsonify }}; Defensive patterns
Strategy: try-catch
Validate before calling
{{/* sanity-check the resource parses before executeAsTemplate */}}
{{ $r := resources.Get "tpl/site.json" }}
{{ if not $r }}{{ errorf "template resource missing" }}{{ end }} Try / catch
{{ with try (resources.ExecuteAsTemplate "out.json" . $r) }}
{{ with .Err }}{{ errorf "ExecuteAsTemplate failed: %s" . }}{{ end }}
{{ end }} Prevention
- Keep Go-template syntax in the resource valid — mismatched {{ }} is the usual cause
- Escape literal braces in JS/CSS resources that aren't meant as template actions
- Test the resource with `hugo server` after edits; syntax errors surface immediately
When it happens
Trigger: Calling resources.ExecuteAsTemplate (or resources.Get | resources.ExecuteAsTemplate) on a file in assets/ whose contents have malformed Go template actions — unclosed {{ }}, unknown functions at parse time, bad pipeline syntax.
Common situations: Templated CSS/JS/JSON files (e.g. assets/css/vars.css with {{ .Site.Params... }}) containing a typo; files with literal {{ sequences (e.g. JS template literals, Mustache/Vue syntax) not intended as Go templates; copying snippets from other template languages.
Related errors
- no Resource provided in transformation
- resources in Concat must be of the same Media Type, got %q a
- error building site: %w
- failed to detect format from content
- failed to detect target data serialization format
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/1b15cd199c1afbba.json.
Report an issue: GitHub ↗.