mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Made it possible to use custom functions in HTML templates via
template.FuncMap
This commit is contained in:
parent
3900df04d2
commit
2de0f29128
21
gin.go
21
gin.go
@ -45,6 +45,7 @@ type (
|
||||
Engine struct {
|
||||
RouterGroup
|
||||
HTMLRender render.HTMLRender
|
||||
HTMLFuncs *template.FuncMap
|
||||
allNoRoute HandlersChain
|
||||
allNoMethod HandlersChain
|
||||
noRoute HandlersChain
|
||||
@ -122,21 +123,27 @@ func (engine *Engine) allocateContext() *Context {
|
||||
}
|
||||
|
||||
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||
if engine.HTMLFuncs == nil {
|
||||
engine.HTMLFuncs = &template.FuncMap{}
|
||||
}
|
||||
|
||||
if IsDebugging() {
|
||||
debugPrintLoadTemplate(template.Must(template.ParseGlob(pattern)))
|
||||
engine.HTMLRender = render.HTMLDebug{Glob: pattern}
|
||||
debugPrintLoadTemplate(template.Must(template.New("main").Funcs(*engine.HTMLFuncs).ParseGlob(pattern)))
|
||||
engine.HTMLRender = render.HTMLDebug{Glob: pattern, Fmap: engine.HTMLFuncs}
|
||||
} else {
|
||||
templ := template.Must(template.ParseGlob(pattern))
|
||||
engine.SetHTMLTemplate(templ)
|
||||
engine.SetHTMLTemplate(template.Must(template.New("main").Funcs(*engine.HTMLFuncs).ParseGlob(pattern)))
|
||||
}
|
||||
}
|
||||
|
||||
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||
if engine.HTMLFuncs == nil {
|
||||
engine.HTMLFuncs = &template.FuncMap{}
|
||||
}
|
||||
|
||||
if IsDebugging() {
|
||||
engine.HTMLRender = render.HTMLDebug{Files: files}
|
||||
engine.HTMLRender = render.HTMLDebug{Files: files, Fmap: engine.HTMLFuncs}
|
||||
} else {
|
||||
templ := template.Must(template.ParseFiles(files...))
|
||||
engine.SetHTMLTemplate(templ)
|
||||
engine.SetHTMLTemplate(template.Must(template.New("main").Funcs(*engine.HTMLFuncs).ParseFiles(files...)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,11 +16,13 @@ type (
|
||||
|
||||
HTMLProduction struct {
|
||||
Template *template.Template
|
||||
Fmap *template.FuncMap
|
||||
}
|
||||
|
||||
HTMLDebug struct {
|
||||
Files []string
|
||||
Glob string
|
||||
Fmap *template.FuncMap
|
||||
}
|
||||
|
||||
HTML struct {
|
||||
@ -47,13 +49,15 @@ func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||
if len(r.Files) > 0 {
|
||||
return template.Must(template.ParseFiles(r.Files...))
|
||||
return template.Must(template.New("main").Funcs(*r.Fmap).ParseFiles(r.Files...))
|
||||
}
|
||||
if len(r.Glob) > 0 {
|
||||
return template.Must(template.ParseGlob(r.Glob))
|
||||
return template.Must(template.New("main").Funcs(*r.Fmap).ParseGlob(r.Glob))
|
||||
}
|
||||
|
||||
panic("the HTML debug render was created without files or glob pattern")
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"encoding/xml"
|
||||
"html/template"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -128,3 +129,22 @@ func TestRenderHTMLTemplate(t *testing.T) {
|
||||
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
|
||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
||||
}
|
||||
|
||||
func TestRenderHTMLTemplateWithFuncs(t *testing.T) {
|
||||
funcs := template.FuncMap{
|
||||
"F": func(in string) string { return strings.ToLower(in) },
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
templ := template.Must(template.New("t").Funcs(funcs).Parse(`HELLO {{.name | F}}`))
|
||||
|
||||
htmlRender := HTMLProduction{Template: templ, Fmap: &funcs}
|
||||
instance := htmlRender.Instance("t", map[string]interface{}{
|
||||
"name": "ALEXANDERNYQUIST",
|
||||
})
|
||||
|
||||
err := instance.Render(w)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, w.Body.String(), "HELLO alexandernyquist")
|
||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user