Skip to content

Commit 717a636

Browse files
committed
Add Sprig and other tpl functions to static templates as well. Closes #1527.
1 parent 7019f26 commit 717a636

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

cmd/init.go

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -605,33 +605,7 @@ func initMediaStore() media.Store {
605605
// initNotifTemplates compiles and returns e-mail notification templates that are
606606
// used for sending ad-hoc notifications to admins and subscribers.
607607
func initNotifTemplates(path string, fs stuffbin.FileSystem, i *i18n.I18n, cs *constants) *notifTpls {
608-
// Register utility functions that the e-mail templates can use.
609-
funcs := template.FuncMap{
610-
"RootURL": func() string {
611-
return cs.RootURL
612-
},
613-
"LogoURL": func() string {
614-
return cs.LogoURL
615-
},
616-
"Date": func(layout string) string {
617-
if layout == "" {
618-
layout = time.ANSIC
619-
}
620-
return time.Now().Format(layout)
621-
},
622-
"L": func() *i18n.I18n {
623-
return i
624-
},
625-
"Safe": func(safeHTML string) template.HTML {
626-
return template.HTML(safeHTML)
627-
},
628-
}
629-
630-
for k, v := range sprig.GenericFuncMap() {
631-
funcs[k] = v
632-
}
633-
634-
tpls, err := stuffbin.ParseTemplatesGlob(funcs, fs, "/static/email-templates/*.html")
608+
tpls, err := stuffbin.ParseTemplatesGlob(initTplFuncs(i, cs), fs, "/static/email-templates/*.html")
635609
if err != nil {
636610
lo.Fatalf("error parsing e-mail notif templates: %v", err)
637611
}
@@ -760,11 +734,7 @@ func initHTTPServer(app *App) *echo.Echo {
760734
}
761735
})
762736

763-
// Parse and load user facing templates.
764-
tpl, err := stuffbin.ParseTemplatesGlob(template.FuncMap{
765-
"L": func() *i18n.I18n {
766-
return app.i18n
767-
}}, app.fs, "/public/templates/*.html")
737+
tpl, err := stuffbin.ParseTemplatesGlob(initTplFuncs(app.i18n, app.constants), app.fs, "/public/templates/*.html")
768738
if err != nil {
769739
lo.Fatalf("error parsing public templates: %v", err)
770740
}
@@ -858,3 +828,32 @@ func joinFSPaths(root string, paths []string) []string {
858828

859829
return out
860830
}
831+
832+
func initTplFuncs(i *i18n.I18n, cs *constants) template.FuncMap {
833+
funcs := template.FuncMap{
834+
"RootURL": func() string {
835+
return cs.RootURL
836+
},
837+
"LogoURL": func() string {
838+
return cs.LogoURL
839+
},
840+
"Date": func(layout string) string {
841+
if layout == "" {
842+
layout = time.ANSIC
843+
}
844+
return time.Now().Format(layout)
845+
},
846+
"L": func() *i18n.I18n {
847+
return i
848+
},
849+
"Safe": func(safeHTML string) template.HTML {
850+
return template.HTML(safeHTML)
851+
},
852+
}
853+
854+
for k, v := range sprig.GenericFuncMap() {
855+
funcs[k] = v
856+
}
857+
858+
return funcs
859+
}

0 commit comments

Comments
 (0)