{"record":{"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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/hugolib/page__per_output.go#L105-L141","documentation":"Returned by pageContentOutput.Render (page__per_output.go:123) when the resolved layout template exists but failed during execution. The template was located successfully but executeToString raised a runtime error while rendering it against the page; that error is wrapped with the template's Name().","triggerScenarios":"The layout references an undefined method or field on the page object; calls a template func with wrong argument count/types; divides by zero; accesses nil in a way the Go template engine rejects; a partial invoked from the layout errors.","commonSituations":"Refactoring page methods and forgetting to update layouts; a content adapter page missing fields a normal page has; partial template bugs that only surface for certain content shapes; version upgrades where a method signature changed.","solutions":["Read the wrapped error — it identifies the template name and the runtime cause; fix the offending expression in that layout.","Run `hugo --templateMetricsHints` and check the named template for nil/unsafe access; add `with`/`if` guards.","Reproduce with a minimal content file and use `hugo server --disableFastRender` to get a full stack."],"exampleFix":"{{/* before: .Author may be nil */}}\n{{ .Author.Name }}\n\n{{/* after */}}\n{{ with .Author }}{{ .Name }}{{ end }}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"{{/* Guard against nil fields that commonly cause execution errors */}}\n{{ with .Author }}{{ .Name }}{{ end }}\n{{ with .Params.cover }}{{ .url }}{{ end }}","tryCatchPattern":"{{/* Wrap .Render so failures degrade gracefully in dev */}}\n{{ $html := \"\" }}\n{{ with .Render \"mylayout\" }}{{ $html = . }}{{ else }}\n  {{ warnf \"layout 'mylayout' failed for %s\" .File.Path }}\n{{ end }}","preventionTips":["Always use `with`/`if` guards for optional fields in layouts.","Run `hugo --templateMetricsHints` to spot expensive/risky templates.","Add a smoke-test build in CI that fails on any template execution error."],"tags":["templates","render","runtime"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}