knadh/listmonk · error

error compiling subject: %v

Error message

error compiling subject: %v

What it means

Compilation of a campaign's subject template failed during campaign message templating, usually due to invalid template syntax in the subject.

Source

Thrown at models/campaigns.go:152

	}

	return nil
}

// CompileTemplate compiles a campaign body template into its base
// template and sets the resultant template to Campaign.Tpl.
func (c *Campaign) CompileTemplate(f template.FuncMap) error {
	// If the subject line has a template string, compile it.
	if hasTplExpr(c.Subject) {
		subj := c.Subject
		for _, r := range regTplFuncs {
			subj = r.regExp.ReplaceAllString(subj, r.replace)
		}

		var txtFuncs map[string]any = f
		subjTpl, err := txttpl.New(ContentTpl).Funcs(txtFuncs).Parse(subj)
		if err != nil {
			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)

View on GitHub (pinned to 670c01717d)

Solutions

  1. Check the wrapped %v error for the exact parse position and fix the subject template syntax
  2. Escape literal braces: use {{"{{"}}" to print a literal "{{"
  3. Verify any function called in the subject exists in the registered template funcs
  4. Preview the subject via previewTemplate to validate before saving

Example fix

// before
subject = "Hi {{ .SubscriberName "
// after
subject = "Hi {{ .SubscriberName }}"
Defensive patterns

Strategy: validation

Validate before calling

if _, err := txttpl.New("t").Parse(subject); err != nil {
    return fmt.Errorf("invalid subject template: %v", err)
}

Try / catch

if err := c.CompileTemplate(); err != nil {
    if strings.Contains(err.Error(), "error compiling subject") {
        // show the subject editor with the parse error highlighted
    }
}

Prevention

When it happens

Trigger: Calling CompileTemplate when the campaign Subject contains invalid template syntax — unbalanced {{ }}, unknown function, bad pipeline, e.g. "{{ .Subject" or "{{ unknownFunc x }}".

Common situations: Hand-edited subject with a stray "{{"; using a template function name that doesn't exist (typo); literal "{{" in a subject not escaped with {{"{{"}}"; variables referencing undefined fields.

Related errors


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