{"record":{"id":"47640f72e2ff10d5","repo":"charmbracelet/gum","slug":"unable-to-parse-template-w","errorCode":null,"errorMessage":"unable to parse template: %w","messagePattern":"unable to parse template: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"format/formats.go","lineNumber":60,"sourceCode":"func markdown(input string, theme string) (string, error) {\n\trenderer, err := glamour.NewTermRenderer(\n\t\tglamour.WithStylePath(theme),\n\t\tglamour.WithWordWrap(0),\n\t)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to render: %w\", err)\n\t}\n\toutput, err := renderer.Render(input)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to render: %w\", err)\n\t}\n\treturn output, nil\n}\n\nfunc template(input string) (string, error) {\n\tt, err := tpl.New(\"tpl\").Funcs(templateFuncs()).Parse(input)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to parse template: %w\", err)\n\t}\n\n\tvar buf bytes.Buffer\n\terr = t.Execute(&buf, nil)\n\treturn buf.String(), err\n}\n\n// templateFuncs returns a few useful template helpers for styling text.\nfunc templateFuncs() tpl.FuncMap {\n\treturn tpl.FuncMap{\n\t\t\"Color\": func(values ...any) string {\n\t\t\ts := lipgloss.NewStyle()\n\t\t\tswitch len(values) {\n\t\t\tcase 2:\n\t\t\t\ts = s.Foreground(lipgloss.Color(values[0].(string)))\n\t\t\tcase 3:\n\t\t\t\ts = s.\n\t\t\t\t\tForeground(lipgloss.Color(values[0].(string))).","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/charmbracelet/gum/blob/4d089f95507708a71f64dacfe7ca513219dd5267/format/formats.go#L42-L78","documentation":"Returned by the template formatter when text/template (via the tpl wrapper) cannot Parse the user-supplied template string. This is a compile-time template error: bad syntax, unknown functions, or unbalanced actions. The wrapped error includes the template parser's position information.","triggerScenarios":"Calling Run with Format=template and input containing invalid template syntax such as {{.Field} on a nil-safe template, unknown function names not in templateFuncs(), or unclosed {{ }} actions.","commonSituations":"Shell interpolation mangling braces; referencing a function that doesn't exist in the template func map; quoting issues causing {{ to be split; data fields referenced although Execute is called with nil data.","solutions":["Check template syntax: every {{ }} action must be closed and functions must exist in the template func map","Wrap the template in single quotes in the shell so braces are not mangled","Only reference available template functions (see templateFuncs in the repo)","Remember Execute is called with nil data — do not dereference . fields"],"exampleFix":"// before\ngum format --type template '{{ .Name | upper'\n// after\ngum format --type template '{{ upper \"hello\" }}'","handlingStrategy":"try-catch","validationCode":"if strings.Count(tpl, \"{{\") != strings.Count(tpl, \"}}\") { return fmt.Errorf(\"unbalanced template braces\") }","typeGuard":"func isBalancedTemplate(t string) bool { return strings.Count(t, \"{{\") == strings.Count(t, \"}}\") }","tryCatchPattern":"out, err := format.Format(tpl, format.Template)\nif err != nil {\n    return fmt.Errorf(\"template error: %w\", err)\n}","preventionTips":["Quote templates in single quotes in shell to protect braces","Only use functions present in templateFuncs()","Remember Execute runs with nil data — avoid .field references","Validate brace balance before invoking"],"tags":["go","template","text-template","parsing"],"backgroundTag":"template-parse-error","analyzedSha":"4d089f95507708a71f64dacfe7ca513219dd5267","analyzedAt":"2026-08-31T18:45:06.436Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}