{"id":"ccefe1b5e6884424","repo":"gohugoio/hugo","slug":"failed-to-execute-template-s-w","errorCode":null,"errorMessage":"failed to execute template %s: %w","messagePattern":"failed to execute template (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hugolib/page__per_output.go","lineNumber":123,"sourceCode":"}\n\nfunc (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) {\n\tif len(layout) == 0 {\n\t\treturn \"\", errors.New(\"no layout given\")\n\t}\n\ttempl, found, err := pco.po.p.resolveTemplate(layout...)\n\tif err != nil {\n\t\treturn \"\", pco.po.p.wrapError(err)\n\t}\n\n\tif !found {\n\t\treturn \"\", fmt.Errorf(\"template %q not found\", layout[0])\n\t}\n\n\t// Make sure to send the *pageState and not the *pageContentOutput to the template.\n\tres, err := executeToString(ctx, pco.po.p.s.GetTemplateStore(), templ, pco.po.p)\n\tif err != nil {\n\t\treturn \"\", pco.po.p.wrapError(fmt.Errorf(\"failed to execute template %s: %w\", templ.Name(), err))\n\t}\n\treturn template.HTML(res), nil\n}\n\nfunc (pco *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Fragments {\n\treturn pco.c().Fragments(ctx)\n}\n\nfunc (pco *pageContentOutput) RenderShortcodes(ctx context.Context) (template.HTML, error) {\n\treturn pco.c().RenderShortcodes(ctx)\n}\n\nfunc (pco *pageContentOutput) Markup(opts ...any) page.Markup {\n\tif len(opts) > 1 {\n\t\tpanic(\"too many arguments, expected 0 or 1\")\n\t}\n\tvar scope string\n\tif len(opts) == 1 {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/hugolib/page__per_output.go#L105-L141","documentation":"Returned by pageContentOutput.Render when the resolved layout template was found but executing it against the page failed. The underlying template execution error (nil pointer method call, bad function argument, error from a partial, etc.) is wrapped with the template name and the page's file context via wrapError, so the root cause is in the %w-wrapped inner error, not this message.","triggerScenarios":"`{{ .Render \"summary\" }}` where summary.html itself errors at execution time — calling a method on a nil value, wrong argument types to template funcs, a failing partial, or an error returned by resources/transform calls inside the layout.","commonSituations":"Templates assuming params exist (`.Params.foo.bar` on nil), date/format calls on missing fields, partials that error only for certain pages (e.g. pages without images), or upgrades where a template function's signature or behavior changed.","solutions":["Read the wrapped inner error — it names the exact template line and failure; fix that expression in the named layout file.","Guard optional data with `with`/`if` in the layout, e.g. `{{ with .Params.image }}...{{ end }}`.","Reproduce with `hugo -v` or building just the failing section to see the full error chain including file position."],"exampleFix":"<!-- before (layouts/summary.html) -->\n<img src=\"{{ .Params.image.url }}\">\n<!-- after -->\n{{ with .Params.image }}<img src=\"{{ .url }}\">{{ end }}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"var fe herrors.FileError\nisTemplateErr := errors.As(err, &fe)","tryCatchPattern":"// Programmatic embedders: template execution failures surface from the build\nif err := b.Build(hugolib.BuildCfg{}); err != nil {\n    var fe herrors.FileError\n    if errors.As(err, &fe) {\n        log.Printf(\"template error at %s: %v\", fe.Position(), fe)\n    }\n    return err\n}","preventionTips":["Guard nil-prone accesses in templates with with/if before calling methods on .Params values","Test templates against pages with minimal front matter — execution errors usually come from missing fields","Read the file:line position in the wrapped error; it points into the failing template"],"tags":["hugo","templates","execution","render"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}