charmbracelet/gum · error

unable to parse template: %w

Error message

unable to parse template: %w

What it means

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.

Source

Thrown at format/formats.go:60

func markdown(input string, theme string) (string, error) {
	renderer, err := glamour.NewTermRenderer(
		glamour.WithStylePath(theme),
		glamour.WithWordWrap(0),
	)
	if err != nil {
		return "", fmt.Errorf("unable to render: %w", err)
	}
	output, err := renderer.Render(input)
	if err != nil {
		return "", fmt.Errorf("unable to render: %w", err)
	}
	return output, nil
}

func template(input string) (string, error) {
	t, err := tpl.New("tpl").Funcs(templateFuncs()).Parse(input)
	if err != nil {
		return "", fmt.Errorf("unable to parse template: %w", err)
	}

	var buf bytes.Buffer
	err = t.Execute(&buf, nil)
	return buf.String(), err
}

// templateFuncs returns a few useful template helpers for styling text.
func templateFuncs() tpl.FuncMap {
	return tpl.FuncMap{
		"Color": func(values ...any) string {
			s := lipgloss.NewStyle()
			switch len(values) {
			case 2:
				s = s.Foreground(lipgloss.Color(values[0].(string)))
			case 3:
				s = s.
					Foreground(lipgloss.Color(values[0].(string))).

View on GitHub (pinned to 4d089f9550)

Solutions

  1. Check template syntax: every {{ }} action must be closed and functions must exist in the template func map
  2. Wrap the template in single quotes in the shell so braces are not mangled
  3. Only reference available template functions (see templateFuncs in the repo)
  4. Remember Execute is called with nil data — do not dereference . fields

Example fix

// before
gum format --type template '{{ .Name | upper'
// after
gum format --type template '{{ upper "hello" }}'
Defensive patterns

Strategy: try-catch

Validate before calling

if strings.Count(tpl, "{{") != strings.Count(tpl, "}}") { return fmt.Errorf("unbalanced template braces") }

Type guard

func isBalancedTemplate(t string) bool { return strings.Count(t, "{{") == strings.Count(t, "}}") }

Try / catch

out, err := format.Format(tpl, format.Template)
if err != nil {
    return fmt.Errorf("template error: %w", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of charmbracelet/gum@4d089f9550 (2026-08-31). Data as JSON: /api/errors/47640f72e2ff10d5. Report an issue: GitHub.