mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 14:12:16 +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 {
|
Engine struct {
|
||||||
RouterGroup
|
RouterGroup
|
||||||
HTMLRender render.HTMLRender
|
HTMLRender render.HTMLRender
|
||||||
|
HTMLFuncs *template.FuncMap
|
||||||
allNoRoute HandlersChain
|
allNoRoute HandlersChain
|
||||||
allNoMethod HandlersChain
|
allNoMethod HandlersChain
|
||||||
noRoute HandlersChain
|
noRoute HandlersChain
|
||||||
@ -122,21 +123,27 @@ func (engine *Engine) allocateContext() *Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
|
if engine.HTMLFuncs == nil {
|
||||||
|
engine.HTMLFuncs = &template.FuncMap{}
|
||||||
|
}
|
||||||
|
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
debugPrintLoadTemplate(template.Must(template.ParseGlob(pattern)))
|
debugPrintLoadTemplate(template.Must(template.New("main").Funcs(*engine.HTMLFuncs).ParseGlob(pattern)))
|
||||||
engine.HTMLRender = render.HTMLDebug{Glob: pattern}
|
engine.HTMLRender = render.HTMLDebug{Glob: pattern, Fmap: engine.HTMLFuncs}
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.ParseGlob(pattern))
|
engine.SetHTMLTemplate(template.Must(template.New("main").Funcs(*engine.HTMLFuncs).ParseGlob(pattern)))
|
||||||
engine.SetHTMLTemplate(templ)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
|
if engine.HTMLFuncs == nil {
|
||||||
|
engine.HTMLFuncs = &template.FuncMap{}
|
||||||
|
}
|
||||||
|
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
engine.HTMLRender = render.HTMLDebug{Files: files}
|
engine.HTMLRender = render.HTMLDebug{Files: files, Fmap: engine.HTMLFuncs}
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.ParseFiles(files...))
|
engine.SetHTMLTemplate(template.Must(template.New("main").Funcs(*engine.HTMLFuncs).ParseFiles(files...)))
|
||||||
engine.SetHTMLTemplate(templ)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,11 +16,13 @@ type (
|
|||||||
|
|
||||||
HTMLProduction struct {
|
HTMLProduction struct {
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
|
Fmap *template.FuncMap
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLDebug struct {
|
HTMLDebug struct {
|
||||||
Files []string
|
Files []string
|
||||||
Glob string
|
Glob string
|
||||||
|
Fmap *template.FuncMap
|
||||||
}
|
}
|
||||||
|
|
||||||
HTML struct {
|
HTML struct {
|
||||||
@ -47,13 +49,15 @@ func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
|||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r HTMLDebug) loadTemplate() *template.Template {
|
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
if len(r.Files) > 0 {
|
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 {
|
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")
|
panic("the HTML debug render was created without files or glob pattern")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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.Body.String(), "Hello alexandernyquist")
|
||||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
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