{"record":{"id":"49ec9d89df69c70a","repo":"gohugoio/hugo","slug":"template-q-is-an-incomplete-or-empty-template","errorCode":null,"errorMessage":"template: %q is an incomplete or empty template","messagePattern":"template: %q is an incomplete or empty template","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/htmltemplate/template.go","lineNumber":103,"sourceCode":"\tif t == nil {\n\t\treturn nil\n\t}\n\tt.nameSpace.mu.Lock()\n\tdefer t.nameSpace.mu.Unlock()\n\tif t.nameSpace.escaped {\n\t\treturn fmt.Errorf(\"html/template: cannot Parse after Execute\")\n\t}\n\treturn nil\n}\n\n// escape escapes all associated templates.\nfunc (t *Template) escape() error {\n\tt.nameSpace.mu.Lock()\n\tdefer t.nameSpace.mu.Unlock()\n\tt.nameSpace.escaped = true\n\tif t.escapeErr == nil {\n\t\tif t.Tree == nil {\n\t\t\treturn fmt.Errorf(\"template: %q is an incomplete or empty template\", t.Name())\n\t\t}\n\t\tif err := escapeTemplate(t, t.text.Root, t.Name()); err != nil {\n\t\t\treturn err\n\t\t}\n\t} else if t.escapeErr != escapeOK {\n\t\treturn t.escapeErr\n\t}\n\treturn nil\n}\n\n// Execute applies a parsed template to the specified data object,\n// writing the output to wr.\n// If an error occurs executing the template or writing its output,\n// execution stops, but partial results may already have been written to\n// the output writer.\n// A template may be executed safely in parallel, although if parallel\n// executions share a Writer the output may be interleaved.\nfunc (t *Template) Execute(wr io.Writer, data any) error {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/htmltemplate/template.go#L85-L121","documentation":"Thrown by escape() (invoked by Execute and Prepare) when escapeErr is nil but t.Tree is nil, i.e. the template object exists in the set but was never given a parsed body. html/template cannot escape an absent parse tree, so it refuses to execute. The %q is the template's Name().","triggerScenarios":"Calling Execute on a Template created via New(name) (or added to a set via t.New) without ever calling Parse/ParseFiles/etc. on it; a template that was registered under a name but whose body parse failed silently.","commonSituations":"Forgetting to parse after New; referencing a template name that was reserved via {{define}} in another file but whose definition was empty or removed; a parse error earlier leaving Tree nil but the template in the set.","solutions":["Ensure Parse (or ParseFiles/ParseGlob/ParseFS) is called with non-empty content for the template before Execute.","Check the error return from Parse; a nil Tree is often the residue of a swallowed parse error.","Verify the template name in Execute matches a template that actually received a parsed body.","Use Lookup(name).Tree != nil as a sanity check before executing."],"exampleFix":"// before\nt := template.New(\"page\")\nerr := t.Execute(w, data) // error: incomplete/empty\n\n// after\nt := template.New(\"page\")\n_, err := t.Parse(bodyHTML)\nif err != nil { return err }\nerr = t.Execute(w, data)","handlingStrategy":"validation","validationCode":"func safeExecute(t *template.Template, w io.Writer, data any) error {\n    if t == nil || t.Tree == nil {\n        return fmt.Errorf(\"template %q has no parsed body\", t.Name())\n    }\n    return t.Execute(w, data)\n}","typeGuard":null,"tryCatchPattern":"if err := t.Execute(w, data); err != nil {\n    if strings.Contains(err.Error(), \"incomplete or empty template\") {\n        // ensure Parse ran; re-parse from source and retry once\n        if _, perr := t.Parse(bodySrc); perr != nil { return perr }\n        err = t.Execute(w, data)\n    }\n    if err != nil { return err }\n}","preventionTips":["Always check Parse's error return; a nil Tree often stems from a swallowed parse error.","After New(name), always follow with a Parse before Execute.","Log DefinedTemplates() at startup to confirm every name has a body."],"tags":["go","html-template","parse","execute"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}