Made funMaps for the templates configurable and hot-reloadable

This commit is contained in:
Thomas Boerger
2017-06-30 21:22:40 +02:00
parent 470fe62637
commit e38c36ee0d
2 changed files with 21 additions and 11 deletions

View File

@ -25,9 +25,10 @@ type (
}
HTMLDebug struct {
Files []string
Glob string
Delims Delims
Files []string
Glob string
Delims Delims
FuncMap template.FuncMap
}
HTML struct {
@ -55,11 +56,14 @@ func (r HTMLDebug) Instance(name string, data interface{}) Render {
}
}
func (r HTMLDebug) loadTemplate() *template.Template {
if r.FuncMap == nil {
r.FuncMap = template.FuncMap{}
}
if len(r.Files) > 0 {
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).ParseFiles(r.Files...))
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
}
if len(r.Glob) > 0 {
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).ParseGlob(r.Glob))
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
}
panic("the HTML debug render was created without files or glob pattern")
}