{"record":{"id":"55ddddf3b80857b8","repo":"gohugoio/hugo","slug":"html-template-q-is-undefined","errorCode":null,"errorMessage":"html/template: %q is undefined","messagePattern":"html/template: %q is undefined","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/htmltemplate/template.go","lineNumber":152,"sourceCode":"// executions share a Writer the output may be interleaved.\nfunc (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error {\n\ttmpl, err := t.lookupAndEscapeTemplate(name)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn tmpl.text.Execute(wr, data)\n}\n\n// lookupAndEscapeTemplate guarantees that the template with the given name\n// is escaped, or returns an error if it cannot be. It returns the named\n// template.\nfunc (t *Template) lookupAndEscapeTemplate(name string) (tmpl *Template, err error) {\n\tt.nameSpace.mu.Lock()\n\tdefer t.nameSpace.mu.Unlock()\n\tt.nameSpace.escaped = true\n\ttmpl = t.set[name]\n\tif tmpl == nil {\n\t\treturn nil, fmt.Errorf(\"html/template: %q is undefined\", name)\n\t}\n\tif tmpl.escapeErr != nil && tmpl.escapeErr != escapeOK {\n\t\treturn nil, tmpl.escapeErr\n\t}\n\tif tmpl.text.Tree == nil || tmpl.text.Root == nil {\n\t\treturn nil, fmt.Errorf(\"html/template: %q is an incomplete template\", name)\n\t}\n\tif t.text.Lookup(name) == nil {\n\t\tpanic(\"html/template internal error: template escaping out of sync\")\n\t}\n\tif tmpl.escapeErr == nil {\n\t\terr = escapeTemplate(tmpl, tmpl.text.Root, name)\n\t}\n\treturn tmpl, err\n}\n\n// DefinedTemplates returns a string listing the defined templates,\n// prefixed by the string \"; defined templates are: \". If there are none,","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/htmltemplate/template.go#L134-L170","documentation":"Returned by lookupAndEscapeTemplate (used by ExecuteTemplate) when no template with the given name exists in the set (t.set[name] == nil). html/template requires the target template to be associated with the caller before it can be executed. The %q is the requested name.","triggerScenarios":"Calling ExecuteTemplate(w, name, data) where name does not match any template registered in the same association set (never defined, typo, or defined in a different template set).","commonSituations":"Typo in the layout/partial name; referencing a partial that lives in a different template set; using a base name vs full path mismatch; templates parsed into separate New() sets rather than one associated set.","solutions":["Verify the exact name passed to ExecuteTemplate matches a {{define}} name or a parsed file's base name in the same set.","Call t.Lookup(name) before ExecuteTemplate and fail with a clear message if nil.","Ensure all partials are parsed into the same template set (associate via t.Parse of shared definitions or t.New).","List defined templates via t.Templates() / t.DefinedTemplates() to confirm available names."],"exampleFix":"// before\nerr := t.ExecuteTemplate(w, \"page\", data) // error: \"page\" undefined\n\n// after\nif t.Lookup(\"page\") == nil {\n    return fmt.Errorf(\"page not in set; have: %s\", t.DefinedTemplates())\n}\nerr := t.ExecuteTemplate(w, \"page\", data)","handlingStrategy":"validation","validationCode":"func executeNamed(t *template.Template, name string, w io.Writer, data any) error {\n    if t.Lookup(name) == nil {\n        return fmt.Errorf(\"template %q undefined; available:%s\", name, t.DefinedTemplates())\n    }\n    return t.ExecuteTemplate(w, name, data)\n}","typeGuard":null,"tryCatchPattern":"if err := t.ExecuteTemplate(w, name, data); err != nil {\n    if strings.Contains(err.Error(), \"is undefined\") {\n        // fall back to a known default template\n        return t.ExecuteTemplate(w, defaultName, data)\n    }\n    return err\n}","preventionTips":["Lookup before ExecuteTemplate and include DefinedTemplates() in the error.","Keep all partials in one associated set.","Use exact base names matching {{define}} names."],"tags":["go","html-template","execute-template","lookup"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}