{"record":{"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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/resources/resource_transformers/templates/execute_as_template.go#L44-L76","documentation":"Returned by the executeAsTemplateTransform when th.TextParse fails to parse the resource's content as a Go text/template. The resource content is read as a string and parsed with the template store's TextParse keyed by the resource's InPath; any template syntax error is wrapped here.","triggerScenarios":"Calling resources.ExecuteAsTemplate (or css.ExecuteAsTemplate / the templates.ExecuteAsTemplate transformer) on a resource whose content contains invalid Go template syntax — unclosed actions, bad delimiters, undefined functions, or malformed pipelines.","commonSituations":"Using ExecuteAsTemplate on a CSS/JS/JSON file that contains '{{' or other template delimiters unintentionally, referencing a function not registered in Hugo's template namespace, or a stray '{{ end }}' / missing '{{ if }}'.","solutions":["Read the wrapped %w cause; it names the line/column of the template parse error inside the resource.","Fix the template syntax in the source asset, or escape literal braces where you did not intend a template action.","Ensure any function used in the template is a valid Hugo/template function name."],"exampleFix":"// before (asset contains invalid action)\n{ \"title\": \"{{ .Title \" }}\n\n// after\n{ \"title\": \"{{ .Title }}\" }","handlingStrategy":"try-catch","validationCode":"// Pre-parse asset as a Go template to surface errors before Hugo build.\nimport \"text/template\"\nif _, err := template.New(\"asset\").Parse(string(content)); err != nil {\n    return fmt.Errorf(\"template parse error in %s: %w\", path, err)\n}","typeGuard":null,"tryCatchPattern":"// Wrap ExecuteAsTemplate and report the wrapped parse error.\nr, err := res.TransformWithContext(ctx, t)\nif err != nil {\n    var pErr *tplParse.Error // if exposed\n    if errors.As(err, &pErr) { /* surface line/col */ }\n    return nil, fmt.Errorf(\"execute-as-template failed: %w\", err)\n}","preventionTips":["Escape literal '{{' '}}' where you don't intend template actions (e.g. CSS/JS).","Only use functions registered in Hugo's template namespace.","Add a CI step that parses templated assets with text/template to catch syntax errors."],"tags":["template","execute-as-template","parse-error","asset-pipeline"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}