mirror of https://github.com/gin-gonic/gin.git
Made funMaps for the templates configurable and hot-reloadable
This commit is contained in:
parent
470fe62637
commit
e38c36ee0d
18
gin.go
18
gin.go
|
@ -47,6 +47,7 @@ type (
|
||||||
RouterGroup
|
RouterGroup
|
||||||
delims render.Delims
|
delims render.Delims
|
||||||
HTMLRender render.HTMLRender
|
HTMLRender render.HTMLRender
|
||||||
|
FuncMap template.FuncMap
|
||||||
allNoRoute HandlersChain
|
allNoRoute HandlersChain
|
||||||
allNoMethod HandlersChain
|
allNoMethod HandlersChain
|
||||||
noRoute HandlersChain
|
noRoute HandlersChain
|
||||||
|
@ -112,6 +113,7 @@ func New() *Engine {
|
||||||
basePath: "/",
|
basePath: "/",
|
||||||
root: true,
|
root: true,
|
||||||
},
|
},
|
||||||
|
FuncMap: template.FuncMap{},
|
||||||
RedirectTrailingSlash: true,
|
RedirectTrailingSlash: true,
|
||||||
RedirectFixedPath: false,
|
RedirectFixedPath: false,
|
||||||
HandleMethodNotAllowed: false,
|
HandleMethodNotAllowed: false,
|
||||||
|
@ -147,19 +149,19 @@ func (engine *Engine) Delims(left, right string) *Engine {
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
debugPrintLoadTemplate(template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseGlob(pattern)))
|
debugPrintLoadTemplate(template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseGlob(pattern)))
|
||||||
engine.HTMLRender = render.HTMLDebug{Glob: pattern, Delims: engine.delims}
|
engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseGlob(pattern))
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseGlob(pattern))
|
||||||
engine.SetHTMLTemplate(templ)
|
engine.SetHTMLTemplate(templ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
engine.HTMLRender = render.HTMLDebug{Files: files, Delims: engine.delims}
|
engine.HTMLRender = render.HTMLDebug{Files: files, FuncMap: engine.FuncMap, Delims: engine.delims}
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseFiles(files...))
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFiles(files...))
|
||||||
engine.SetHTMLTemplate(templ)
|
engine.SetHTMLTemplate(templ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,7 +171,11 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
|
||||||
debugPrintWARNINGSetHTMLTemplate()
|
debugPrintWARNINGSetHTMLTemplate()
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.HTMLRender = render.HTMLProduction{Template: templ}
|
engine.HTMLRender = render.HTMLProduction{Template: templ.Funcs(engine.FuncMap)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) SetFuncMap(funcMap template.FuncMap) {
|
||||||
|
engine.FuncMap = funcMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
|
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
|
||||||
|
|
|
@ -28,6 +28,7 @@ type (
|
||||||
Files []string
|
Files []string
|
||||||
Glob string
|
Glob string
|
||||||
Delims Delims
|
Delims Delims
|
||||||
|
FuncMap template.FuncMap
|
||||||
}
|
}
|
||||||
|
|
||||||
HTML struct {
|
HTML struct {
|
||||||
|
@ -55,11 +56,14 @@ func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (r HTMLDebug) loadTemplate() *template.Template {
|
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
|
if r.FuncMap == nil {
|
||||||
|
r.FuncMap = template.FuncMap{}
|
||||||
|
}
|
||||||
if len(r.Files) > 0 {
|
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 {
|
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")
|
panic("the HTML debug render was created without files or glob pattern")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue