{"record":{"id":"7a8ebc641b977447","repo":"gohugoio/hugo","slug":"html-template-q-is-an-incomplete-template","errorCode":null,"errorMessage":"html/template: %q is an incomplete template","messagePattern":"html/template: %q is an incomplete template","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/htmltemplate/template.go","lineNumber":158,"sourceCode":"\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,\n// it returns the empty string. Used to generate an error message.\nfunc (t *Template) DefinedTemplates() string {\n\treturn t.text.DefinedTemplates()\n}\n\n// Parse parses text as a template body for t.","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/htmltemplate/template.go#L140-L176","documentation":"Returned by lookupAndEscapeTemplate when the named template IS in the set but its text.Tree or text.Root is nil, meaning it has no parse tree to execute. This differs from \"undefined\" (663): the template entry exists but has no body. The %q is the requested name.","triggerScenarios":"Calling ExecuteTemplate on a template that was created (e.g. via t.New(name)) and added to the set but never parsed, or whose definition body was only whitespace/comments and got dropped, leaving Tree nil.","commonSituations":"A {{define \"x\"}}{{end}} with empty body that was treated as empty; a New(name) reservation never followed by Parse; a partial file that is empty or contains only comments; a template whose Parse failed but the name was already registered.","solutions":["Provide actual non-empty parsed content for the named template before executing it.","Check t.Lookup(name) and confirm its underlying text template has a non-nil Tree/Root before ExecuteTemplate.","Remove or skip empty placeholder definitions; render a fallback instead.","Audit parse results to ensure every define in the set received a body."],"exampleFix":"// before\nt.New(\"empty\")\nerr := t.ExecuteTemplate(w, \"empty\", data) // error: incomplete\n\n// after\nt.New(\"empty\")\n_, err := t.Lookup(\"empty\").Parse(`<p>{{.}}</p>`)\nif err != nil { return err }\nerr = t.ExecuteTemplate(w, \"empty\", data)","handlingStrategy":"validation","validationCode":"func executeComplete(t *template.Template, name string, w io.Writer, data any) error {\n    sub := t.Lookup(name)\n    if sub == nil || sub.Tree == nil {\n        return fmt.Errorf(\"template %q incomplete; parse a body first\", name)\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 an incomplete template\") {\n        if sub := t.Lookup(name); sub != nil {\n            if _, perr := sub.Parse(fallbackBody); perr == nil {\n                return t.ExecuteTemplate(w, name, data)\n            }\n        }\n    }\n    return err\n}","preventionTips":["Never leave a {{define}} with an empty body that you intend to execute.","After t.New(name), always Parse a body before that name is executed.","Validate each template in the set has a non-nil Tree at startup."],"tags":["go","html-template","execute-template","parse"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}