{"id":"3cceeefa5d064b10","repo":"gohugoio/hugo","slug":"failed-to-parse-archetype-template-s-w","errorCode":null,"errorMessage":"failed to parse archetype template: %s: %w","messagePattern":"failed to parse archetype template: (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hugolib/content_factory.go","lineNumber":77,"sourceCode":"// ApplyArchetypeTemplate templateSource to w as a template using the given Page p as the foundation for the data context.\nfunc (f ContentFactory) ApplyArchetypeTemplate(w io.Writer, p page.Page, archetypeKind, templateSource string) error {\n\tps := p.(*pageState)\n\tif archetypeKind == \"\" {\n\t\tarchetypeKind = p.Type()\n\t}\n\n\td := &archetypeFileData{\n\t\tType: archetypeKind,\n\t\tDate: htime.Now().Format(time.RFC3339),\n\t\tPage: p,\n\t\tFile: p.File(),\n\t}\n\n\ttemplateSource = f.shortcodeReplacerPre.Replace(templateSource)\n\n\ttempl, err := ps.s.TemplateStore.TextParse(\"archetype.md\", templateSource)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse archetype template: %s: %w\", err, err)\n\t}\n\n\tresult, err := executeToString(context.Background(), ps.s.GetTemplateStore(), templ, d)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to execute archetype template: %s: %w\", err, err)\n\t}\n\n\t_, err = io.WriteString(w, f.shortcodeReplacerPost.Replace(result))\n\n\treturn err\n}\n\nfunc (f ContentFactory) SectionFromFilename(filename string) (string, error) {\n\tfilename = filepath.Clean(filename)\n\trel, _, err := f.h.AbsProjectContentDir(filename)\n\tif err != nil {\n\t\treturn \"\", err\n\t}","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/hugolib/content_factory.go#L59-L95","documentation":"Archetype files are parsed as Go text templates (after shortcodes are temporarily replaced with placeholders) so that fields like {{ .Date }} and {{ .File.ContentBaseName }} can be expanded when creating new content. If TemplateStore.TextParse fails, the archetype contains invalid Go template syntax and `hugo new` aborts.","triggerScenarios":"`hugo new content <path>` with an archetype containing malformed template actions — unclosed `{{`, unknown functions, bad pipeline syntax, or literal `{{` sequences (e.g. example code in the archetype body) that aren't escaped.","commonSituations":"Archetypes documenting template snippets that get interpreted, copy-pasted front matter with unbalanced braces, using functions unavailable in the archetype context, or Jinja/Liquid syntax from other generators pasted into a Hugo archetype.","solutions":["Fix the template syntax at the position given in the parse error (unclosed action, unknown function, etc.).","Escape literal braces intended as text: use `{{ \"{{\" }}` or wrap example code so it isn't parsed.","Note shortcodes (`{{< ... >}}`/`{{% ... %}}`) are safe — they're placeholder-replaced — but plain `{{ ... }}` must be valid Go template code.","Temporarily simplify the archetype to isolate the offending line."],"exampleFix":"<!-- before: archetypes/posts.md -->\n---\ntitle: \"{{ replace .File.ContentBaseName \"-\" \" \" | title }\"\n---\n<!-- after -->\n---\ntitle: \"{{ replace .File.ContentBaseName \"-\" \" \" | title }}\"\n---","handlingStrategy":"try-catch","validationCode":"// Pre-parse archetype templates in CI to catch syntax errors\nb, _ := os.ReadFile(archetype)\nif _, err := template.New(\"a\").Funcs(funcMap).Parse(string(b)); err != nil {\n    return fmt.Errorf(\"bad archetype template %s: %w\", archetype, err)\n}","typeGuard":null,"tryCatchPattern":"if err := create.NewContent(h, kind, target); err != nil {\n    if strings.Contains(err.Error(), \"failed to parse archetype template\") {\n        // fix Go template syntax in the archetype (unclosed {{ }}, unknown function)\n    }\n    return err\n}","preventionTips":["Archetypes are Go templates — balance every `{{` with `}}` and only use functions available in the archetype context (.Name, .Date, site, time, etc.)","Escape literal `{{` in example content with `{{\"{{\"}}` or template comments","Run `hugo new test-content.md` in CI whenever archetypes change to catch parse errors before users hit them"],"tags":["hugo","archetypes","go-templates","template-syntax"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}