knadh/listmonk · error

error inserting child template: %v

Error message

error inserting child template: %v

What it means

After parsing the body template, CompileTemplate attaches its parse tree into the base template via AddParseTree(ContentTpl, ...). This fails if the trees are incompatible (e.g. a template of the internal ContentTpl name already defined in baseTPL, or a conflicting tree).

Source

Thrown at models/campaigns.go:196

		}
		body = b.String()
	} else {
		body = c.Body
	}

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

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

	out, err := baseTPL.AddParseTree(ContentTpl, msgTpl.Tree)
	if err != nil {
		return fmt.Errorf("error inserting child template: %v", err)
	}
	c.Tpl = out

	if hasTplExpr(c.AltBody.String) {
		b := c.AltBody.String
		for _, r := range regTplFuncs {
			b = r.regExp.ReplaceAllString(b, r.replace)
		}
		bTpl, err := template.New(ContentTpl).Funcs(f).Parse(b)
		if err != nil {
			return fmt.Errorf("error compiling alt plaintext message: %v", err)
		}
		c.AltBodyTpl = bTpl
	}

	// Compile any header values that contain template expressions.
	for _, set := range c.Headers {
		for _, val := range set {

View on GitHub (pinned to 670c01717d)

Solutions

  1. Remove or rename any {{ define "..." }} blocks in the campaign body that could collide with the internal ContentTpl name
  2. Keep the body as a single plain template without define blocks
  3. Check the Go version's AddParseTree semantics if behavior changed after an upgrade

Example fix

// before
body = "{{ define \"content\" }}Hi{{ end }}"
// after
body = "Hi"
Defensive patterns

Strategy: validation

Validate before calling

if strings.Contains(body, "{{ define") {
    return errors.New("body must not contain {{ define }} blocks")
}

Try / catch

if err := c.CompileTemplate(); err != nil {
    if strings.Contains(err.Error(), "error inserting child template") {
        // strip define blocks from the body and recompile
    }
}

Prevention

When it happens

Trigger: baseTPL.AddParseTree(ContentTpl, msgTpl.Tree) returns an error — typically because the body contains {{ define "..." }} blocks that collide with the internal ContentTpl name, or the msgTpl tree is inconsistent.

Common situations: Body contains {{ define "..." }} blocks that collide with the internal ContentTpl name; unusual custom bodies mixing define/inheritance; rarely hit by ordinary campaign bodies.

Related errors


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