{"record":{"id":"f57083abdd26fd1e","repo":"knadh/listmonk","slug":"error-compiling-subject-v","errorCode":null,"errorMessage":"error compiling subject: %v","messagePattern":"error compiling subject: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"models/campaigns.go","lineNumber":152,"sourceCode":"\t}\n\n\treturn nil\n}\n\n// CompileTemplate compiles a campaign body template into its base\n// template and sets the resultant template to Campaign.Tpl.\nfunc (c *Campaign) CompileTemplate(f template.FuncMap) error {\n\t// If the subject line has a template string, compile it.\n\tif hasTplExpr(c.Subject) {\n\t\tsubj := c.Subject\n\t\tfor _, r := range regTplFuncs {\n\t\t\tsubj = r.regExp.ReplaceAllString(subj, r.replace)\n\t\t}\n\n\t\tvar txtFuncs map[string]any = f\n\t\tsubjTpl, err := txttpl.New(ContentTpl).Funcs(txtFuncs).Parse(subj)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error compiling subject: %v\", err)\n\t\t}\n\t\tc.SubjectTpl = subjTpl\n\t}\n\n\t// Compile the base template.\n\tbody := c.TemplateBody\n\n\tif body == \"\" || c.ContentType == CampaignContentTypeVisual {\n\t\tbody = `{{ template \"content\" . }}`\n\t}\n\n\tfor _, r := range regTplFuncs {\n\t\tbody = r.regExp.ReplaceAllString(body, r.replace)\n\t}\n\n\tbaseTPL, err := template.New(BaseTpl).Funcs(f).Parse(body)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error compiling base template: %v\", err)","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/knadh/listmonk/blob/670c01717d48647093335cc23a6be6f4b79c3b6b/models/campaigns.go#L134-L170","documentation":"Compilation of a campaign's subject template failed during campaign message templating, usually due to invalid template syntax in the subject.","triggerScenarios":"Calling CompileTemplate when the campaign Subject contains invalid template syntax — unbalanced {{ }}, unknown function, bad pipeline, e.g. \"{{ .Subject\" or \"{{ unknownFunc x }}\".","commonSituations":"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.","solutions":["Check the wrapped %v error for the exact parse position and fix the subject template syntax","Escape literal braces: use {{\"{{\"}}\" to print a literal \"{{\"","Verify any function called in the subject exists in the registered template funcs","Preview the subject via previewTemplate to validate before saving"],"exampleFix":"// before\nsubject = \"Hi {{ .SubscriberName \"\n// after\nsubject = \"Hi {{ .SubscriberName }}\"","handlingStrategy":"validation","validationCode":"if _, err := txttpl.New(\"t\").Parse(subject); err != nil {\n    return fmt.Errorf(\"invalid subject template: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := c.CompileTemplate(); err != nil {\n    if strings.Contains(err.Error(), \"error compiling subject\") {\n        // show the subject editor with the parse error highlighted\n    }\n}","preventionTips":["Use previewTemplate before saving campaigns","Escape literal braces as {{\"{{\"}}\"","Keep a whitelist of tested template functions and autocomplete them in the UI"],"tags":["template","go","campaign"],"backgroundTag":"template-parse-error","analyzedSha":"670c01717d48647093335cc23a6be6f4b79c3b6b","analyzedAt":"2026-09-01T03:39:35.452Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}