High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Add `IsPreview` to template funcs. Closes #3311.
Kailash Nadha74f40b346ac
diff --git a/cmd/archive.go b/cmd/archive.goindex 1c30906d..cd0ea514 100644--- a/cmd/archive.go+++ b/cmd/archive.go@@ -238,7 +238,7 @@ func (a *App) getCampaignArchives(offset, limit int, renderBody bool) ([]campArc}// compileArchiveCampaigns compiles the campaign template with the subscriber data.-func (a *App) compileArchiveCampaigns(camps []models.Campaign) ([]manager.CampaignMessage, error) {+func (a *App) compileArchiveCampaigns(camps []models.Campaign, opts ...models.CampOpt) ([]manager.CampaignMessage, error) {var (b = bytes.Buffer{}@@ -250,7 +250,7 @@ func (a *App) compileArchiveCampaigns(camps []models.Campaign) ([]manager.Campai// Resolve data-embed images to media URLs as cid: cannot resolve on a web page.a.manager.ApplyArchiveImages(&camp)- if err := camp.CompileTemplate(a.manager.TemplateFuncs(&camp)); err != nil {+ if err := camp.CompileTemplate(a.manager.TemplateFuncs(&camp, opts...)); err != nil {a.log.Printf("error compiling template: %v", err)return nil, echo.NewHTTPError(http.StatusInternalServerError, a.i18n.T("public.errorFetchingCampaign"))}diff --git a/cmd/campaigns.go b/cmd/campaigns.goindex 15637745..428ecca9 100644--- a/cmd/campaigns.go+++ b/cmd/campaigns.go@@ -373,7 +373,7 @@ func (a *App) PreviewCampaign(c *echo.Context) error {// Use a dummy campaign ID to prevent views and clicks from {{ TrackView }}// and {{ TrackLink }} being registered on preview.camp.UUID = dummySubscriber.UUID- if err := camp.CompileTemplate(a.manager.TemplateFuncs(&camp)); err != nil {+ if err := camp.CompileTemplate(a.manager.TemplateFuncs(&camp, models.CampPreview)); err != nil {a.log.Printf("error compiling template: %v", err)return echo.NewHTTPError(http.StatusBadRequest,a.i18n.Ts("templates.errorCompiling", "error", err.Error()))@@ -415,7 +415,7 @@ func (a *App) PreviewCampaignArchive(c *echo.Context) error {camp.ArchiveMeta = json.RawMessage([]byte(c.FormValue("archive_meta")))// "Compile" the campaign template with appropriate data.- res, err := a.compileArchiveCampaigns([]models.Campaign{camp})+ res, err := a.compileArchiveCampaigns([]models.Campaign{camp}, models.CampPreview)if err != nil {return c.Render(http.StatusInternalServerError, tplMessage,makeMsgTpl(a.i18n.T("globals.terms.error"), "", a.i18n.Ts("public.errorFetchingCampaign")))diff --git a/cmd/templates.go b/cmd/templates.goindex d4bf901c..0474ba32 100644--- a/cmd/templates.go+++ b/cmd/templates.go@@ -317,7 +317,7 @@ func (a *App) previewTemplate(tpl models.Template) ([]byte, error) {Body: dummyTpl,}- if err := camp.CompileTemplate(a.manager.TemplateFuncs(&camp)); err != nil {+ if err := camp.CompileTemplate(a.manager.TemplateFuncs(&camp, models.CampPreview)); err != nil {return nil, echo.NewHTTPError(http.StatusBadRequest,a.i18n.Ts("templates.errorCompiling", "error", err.Error()))}@@ -331,7 +331,8 @@ func (a *App) previewTemplate(tpl models.Template) ([]byte, error) {out = msg.Body()} else {// Compile transactional template.- if err := tpl.Compile(a.manager.GenericTemplateFuncs()); err != nil {+ funcs := a.manager.GenericTemplateFuncs(models.CampPreview)+ if err := tpl.Compile(funcs); err != nil {return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error())}@@ -340,7 +341,7 @@ func (a *App) previewTemplate(tpl models.Template) ([]byte, error) {}// Render the message.- if err := m.Render(dummySubscriber, &tpl, a.manager.GenericTemplateFuncs()); err != nil {+ if err := m.Render(dummySubscriber, &tpl, funcs); err != nil {return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error())}out = m.Bodydiff --git a/docs/docs/content/templating.md b/docs/docs/content/templating.mdindex 58d9737e..39569b1c 100644--- a/docs/docs/content/templating.md+++ b/docs/docs/content/templating.md@@ -55,6 +55,7 @@ There are several template functions and expressions that can be used in campaig| `{{ TrackLink "https://link.com" }}` | Takes a URL and generates a tracking URL over it. For use in campaign bodies and templates. || `https://link.com@TrackLink` | Shorthand for `TrackLink`. Eg: `<a href="https://link.com@TrackLink">Link</a>` || `{{ TrackView }}` | Inserts a single tracking pixel. Should only be used once, ideally in the template footer. |+| `{{ IsPreview }}` | Returns `true` when previewing a campaign or template in the editor, and `false` otherwise. || `{{ UnsubscribeURL }}` | Unsubscription and Manage preferences URL. Ideal for use in the template footer. || `{{ UnsubscribeURL }}?manage=true` | Direct URL to Manage Preferences page. || `{{ MessageURL }}` | URL to view the hosted version of an e-mail message. |diff --git a/internal/manager/manager.go b/internal/manager/manager.goindex e30ad4bc..08d322fa 100644--- a/internal/manager/manager.go+++ b/internal/manager/manager.go@@ -11,6 +11,7 @@ import ("net/url""path""regexp"+ "slices""strings""sync""time"@@ -374,7 +375,7 @@ func (m *Manager) GetTpl(id int) (*models.Template, error) {// TemplateFuncs returns the template functions to be applied into// compiled campaign templates.-func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {+func (m *Manager) TemplateFuncs(c *models.Campaign, opts ...models.CampOpt) template.FuncMap {f := template.FuncMap{"TrackLink": func(url string, msg *CampaignMessage) string {if m.cfg.DisableTracking {@@ -425,12 +426,20 @@ func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {},}- maps.Copy(f, m.tplFuncs)+ maps.Copy(f, m.GenericTemplateFuncs(opts...))return f}-func (m *Manager) GenericTemplateFuncs() template.FuncMap {+func (m *Manager) GenericTemplateFuncs(opts ...models.CampOpt) template.FuncMap {+ // Campaign is in preview mode. Override the func.+ if slices.Contains(opts, models.CampPreview) {+ f := maps.Clone(m.tplFuncs)+ f["IsPreview"] = func() bool { return true }++ return f+ }+return m.tplFuncs}@@ -671,6 +680,7 @@ func (m *Manager) sendNotif(c *models.Campaign, status, reason string) error {// functions and sprig template functions.func (m *Manager) makeGnericFuncMap() template.FuncMap {funcs := template.FuncMap{+ "IsPreview": func() bool { return false },"Date": func(layout string) string {if layout == "" {layout = time.ANSICdiff --git a/models/campaigns.go b/models/campaigns.goindex 7280bc9b..e69e8b8d 100644--- a/models/campaigns.go+++ b/models/campaigns.go@@ -31,6 +31,13 @@ const (CampaignContentTypeVisual = "visual")+// CampOpt is a campaign manager option type.+type CampOpt int++const (+ CampPreview CampOpt = iota+)+// Campaigns represents a slice of Campaigns.type Campaigns []Campaign