{"id":"1b15cd199c1afbba","repo":"gohugoio/hugo","slug":"failed-to-parse-resource-q-as-template-w","errorCode":null,"errorMessage":"failed to parse Resource %q as Template:: %w","messagePattern":"failed to parse Resource %q as Template:: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resources/resource_transformers/templates/execute_as_template.go","lineNumber":62,"sourceCode":"}\n\ntype executeAsTemplateTransform struct {\n\trs         *resources.Spec\n\tt          tplimpl.TemplateStoreProvider\n\ttargetPath string\n\tdata       any\n}\n\nfunc (t *executeAsTemplateTransform) Key() internal.ResourceTransformationKey {\n\treturn internal.NewResourceTransformationKey(\"execute-as-template\", t.targetPath)\n}\n\nfunc (t *executeAsTemplateTransform) Transform(ctx *resources.ResourceTransformationCtx) error {\n\ttplStr := helpers.ReaderToString(ctx.From)\n\tth := t.t.GetTemplateStore()\n\tti, err := th.TextParse(ctx.InPath, tplStr)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse Resource %q as Template:: %w\", ctx.InPath, err)\n\t}\n\tctx.OutPath = t.targetPath\n\treturn th.ExecuteWithContext(ctx.Ctx, ti, ctx.To, t.data)\n}\n\nfunc (c *Client) ExecuteAsTemplate(ctx context.Context, res resources.ResourceTransformer, targetPath string, data any) (resource.Resource, error) {\n\treturn res.TransformWithContext(ctx, &executeAsTemplateTransform{\n\t\trs:         c.rs,\n\t\ttargetPath: paths.ToSlashTrimLeading(targetPath),\n\t\tt:          c.t,\n\t\tdata:       data,\n\t})\n}\n","sourceCodeStart":44,"sourceCodeEnd":76,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/resources/resource_transformers/templates/execute_as_template.go#L44-L76","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"<!-- before: assets/js/config.js contains `const t = `${x}` and {{ .Site.Title }` (unclosed) -->\n{{ $r := resources.Get \"js/config.js\" | resources.ExecuteAsTemplate \"js/config.js\" . }}\n<!-- after: fix syntax in the asset -->\nconst title = {{ .Site.Title | jsonify }};","handlingStrategy":"try-catch","validationCode":"{{/* sanity-check the resource parses before executeAsTemplate */}}\n{{ $r := resources.Get \"tpl/site.json\" }}\n{{ if not $r }}{{ errorf \"template resource missing\" }}{{ end }}","typeGuard":null,"tryCatchPattern":"{{ with try (resources.ExecuteAsTemplate \"out.json\" . $r) }}\n  {{ with .Err }}{{ errorf \"ExecuteAsTemplate failed: %s\" . }}{{ end }}\n{{ end }}","preventionTips":["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"],"tags":["templates","parse-error","asset-pipeline","hugo"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}