{"record":{"id":"7bf799cd0ffd89fc","repo":"knadh/listmonk","slug":"error-compiling-subject-v-7bf799","errorCode":null,"errorMessage":"error compiling subject: %v","messagePattern":"error compiling subject: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"models/templates.go","lineNumber":53,"sourceCode":"\tAttachments []Attachment       `json:\"-\"`\n}\n\n// Compile compiles a template body and subject (only for tx templates) and\n// caches the templat references to be executed later.\nfunc (t *Template) Compile(f template.FuncMap) error {\n\ttpl, err := template.New(BaseTpl).Funcs(f).Parse(t.Body)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error compiling transactional template: %v\", err)\n\t}\n\tt.Tpl = tpl\n\n\t// If the subject line has a template string, compile it.\n\tif hasTplExpr(t.Subject) {\n\t\tsubj := t.Subject\n\n\t\tsubjTpl, err := txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(f)).Parse(subj)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error compiling subject: %v\", err)\n\t\t}\n\t\tt.SubjectTpl = subjTpl\n\t}\n\n\treturn nil\n}\n\ntype CampaignStats struct {\n\tID        int       `db:\"id\" json:\"id\"`\n\tStatus    string    `db:\"status\" json:\"status\"`\n\tToSend    int       `db:\"to_send\" json:\"to_send\"`\n\tSent      int       `db:\"sent\" json:\"sent\"`\n\tStarted   null.Time `db:\"started_at\" json:\"started_at\"`\n\tUpdatedAt null.Time `db:\"updated_at\" json:\"updated_at\"`\n\tRate      int       `json:\"rate\"`\n\tNetRate   int       `json:\"net_rate\"`\n}\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/knadh/listmonk/blob/670c01717d48647093335cc23a6be6f4b79c3b6b/models/templates.go#L35-L71","documentation":"This error is returned by Template.Compile in models/templates.go when the template's Subject contains template expressions (hasTplExpr detects {{ ... }}) that fail to parse as a Go text/template. The subject is compiled separately from the body with txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(f)).Parse so it can be rendered per-message; a parse failure is wrapped as \"error compiling subject: %v\". Only tx templates with templated subjects reach this path.","triggerScenarios":"Calling CreateTemplate/UpdateTemplate (or previewTemplate/CacheTpl) on a Template whose Subject is non-empty, contains {{ ... }}, and is syntactically invalid — unclosed action, unknown function name, malformed pipeline, or unbalanced braces.","commonSituations":"Typo in a custom template function inside the subject line, pasting HTML-template pipelines valid only in html/template, stray braces from hand editing the subject in the admin UI, removing/renaming a FuncMap function that existing subject lines still reference.","solutions":["Fix the subject template syntax named in the wrapped parse error (balance braces, close all actions, correct end tags).","Ensure every function used in the subject exists in the FuncMap passed to Compile.","If the subject needs no per-message templating, remove {{ }} expressions so it is stored and used literally.","Validate the subject with text/template.Parse in a quick standalone test before saving the template.","Audit existing subject lines after upgrading the library in case available template functions changed."],"exampleFix":"// before\nt.Subject = \"Receipt for {{ .Subscriber.Name \"\n// after\nt.Subject = \"Receipt for {{ .Subscriber.Name }}\"","handlingStrategy":"validation","validationCode":"import (\"strings\" \"text/template\")\n\nfunc validateTemplateSubject(subject string, funcs template.FuncMap) error {\n\tif subject == \"\" || !strings.Contains(subject, \"{{\") {\n\t\treturn nil\n\t}\n\t_, err := template.New(\"subject\").Funcs(funcs).Parse(subject)\n\treturn err\n}","typeGuard":"func subjectIsTemplated(subject string) bool {\n\treturn strings.Contains(subject, \"{{\") && strings.Contains(subject, \"}}\")\n}","tryCatchPattern":"if err := tpl.Compile(funcs); err != nil {\n\tif strings.HasPrefix(err.Error(), \"error compiling subject\") {\n\t\treturn fmt.Errorf(\"template subject is not valid Go text/template syntax: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Parse-check templated subjects with text/template before saving the template.","Keep subject templates simple: plain field references like {{ .Subscriber.Name }}.","Ensure all subject functions exist in the FuncMap passed to Compile.","Add form/UI-level validation that rejects unbalanced braces in subject inputs.","Re-validate stored templates after upgrading the library in case FuncMap functions changed."],"tags":["go","text-template","template-syntax","compilation"],"backgroundTag":"template-syntax-error","analyzedSha":"670c01717d48647093335cc23a6be6f4b79c3b6b","analyzedAt":"2026-09-01T03:39:35.452Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}