Support multiple HTML globs

This commit is contained in:
David Zbarsky 2019-09-24 11:23:17 -05:00
parent 9b9f4fab34
commit 78e74dc158
2 changed files with 12 additions and 5 deletions

8
gin.go
View File

@ -173,10 +173,14 @@ func (engine *Engine) SecureJsonPrefix(prefix string) *Engine {
// LoadHTMLGlob loads HTML files identified by glob pattern // LoadHTMLGlob loads HTML files identified by glob pattern
// and associates the result with HTML renderer. // and associates the result with HTML renderer.
func (engine *Engine) LoadHTMLGlob(pattern string) { func (engine *Engine) LoadHTMLGlob(pattern ...string) {
left := engine.delims.Left left := engine.delims.Left
right := engine.delims.Right right := engine.delims.Right
templ := template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseGlob(pattern))
templ := template.New("").Delims(left, right).Funcs(engine.FuncMap)
for _, p := range pattern {
templ = template.Must(templ.ParseGlob(p))
}
if IsDebugging() { if IsDebugging() {
debugPrintLoadTemplate(templ) debugPrintLoadTemplate(templ)

View File

@ -32,7 +32,7 @@ type HTMLProduction struct {
// HTMLDebug contains template delims and pattern and function with file list. // HTMLDebug contains template delims and pattern and function with file list.
type HTMLDebug struct { type HTMLDebug struct {
Files []string Files []string
Glob string Glob []string
Delims Delims Delims Delims
FuncMap template.FuncMap FuncMap template.FuncMap
} }
@ -70,8 +70,11 @@ func (r HTMLDebug) loadTemplate() *template.Template {
if len(r.Files) > 0 { if len(r.Files) > 0 {
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...)) return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
} }
if r.Glob != "" { if len(r.Glob) > 0 {
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob)) tmpl := template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap)
for _, g := range r.Glob {
tmpl = template.Must(tmpl.ParseGlob(g))
}
} }
panic("the HTML debug render was created without files or glob pattern") panic("the HTML debug render was created without files or glob pattern")
} }