knadh/listmonk · error

error compiling base template: %v

Error message

error compiling base template: %v

What it means

Compilation of the campaign's base template failed, typically due to invalid template syntax or missing template references.

Source

Thrown at models/campaigns.go:170

			return fmt.Errorf("error compiling subject: %v", err)
		}
		c.SubjectTpl = subjTpl
	}

	// Compile the base template.
	body := c.TemplateBody

	if body == "" || c.ContentType == CampaignContentTypeVisual {
		body = `{{ template "content" . }}`
	}

	for _, r := range regTplFuncs {
		body = r.regExp.ReplaceAllString(body, r.replace)
	}

	baseTPL, err := template.New(BaseTpl).Funcs(f).Parse(body)
	if err != nil {
		return fmt.Errorf("error compiling base template: %v", err)
	}

	// If the format is markdown, convert Markdown to HTML.
	if c.ContentType == CampaignContentTypeMarkdown {
		var b bytes.Buffer
		if err := markdown.Convert([]byte(c.Body), &b); err != nil {
			return err
		}
		body = b.String()
	} else {
		body = c.Body
	}

	// Compile the campaign message.
	for _, r := range regTplFuncs {
		body = r.regExp.ReplaceAllString(body, r.replace)
	}

View on GitHub (pinned to 670c01717d)

Solutions

  1. Read the wrapped parse error for the line/offset and fix the body template syntax
  2. Escape literal braces with {{"{{"}}" / {{"}}"}}
  3. Confirm all invoked template functions exist in the registered func map
  4. Validate the campaign via validateCampaignFields or previewTemplate before sending

Example fix

// before
body = "{{ if .Name }}Hello{{ "
// after
body = "{{ if .Name }}Hello{{ end }}"
Defensive patterns

Strategy: validation

Validate before calling

if _, err := template.New("base").Funcs(funcMap).Parse(body); err != nil {
    return fmt.Errorf("invalid body template: %v", err)
}

Try / catch

if err := c.CompileTemplate(); err != nil {
    if strings.Contains(err.Error(), "error compiling base template") {
        // reject save and point the editor at the failing body line
    }
}

Prevention

When it happens

Trigger: Campaign TemplateBody contains invalid Go template syntax — unclosed action, unknown function, bad pipeline — so template.New(BaseTpl).Funcs(f).Parse(body) fails.

Common situations: Pasting HTML with unescaped "{{" sequences; calling a template func with wrong arity; typos in {{ template "..." }} includes; editing the body in an external editor that mangles braces.

Related errors


AI-assisted analysis of knadh/listmonk@670c01717d (2026-09-01). Data as JSON: /api/errors/3ac72833d89999f2. Report an issue: GitHub.