mirror of https://github.com/gin-gonic/gin.git
Merge pull request #120 from bredov/issue-119
Fix for #119. gin.LoadHTML* incorrectly works in debug mode.
This commit is contained in:
commit
94415333ed
2
gin.go
2
gin.go
|
@ -87,6 +87,7 @@ func Default() *Engine {
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
if gin_mode == debugCode {
|
if gin_mode == debugCode {
|
||||||
|
render.HTMLDebug.AddGlob(pattern)
|
||||||
engine.HTMLRender = render.HTMLDebug
|
engine.HTMLRender = render.HTMLDebug
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.ParseGlob(pattern))
|
templ := template.Must(template.ParseGlob(pattern))
|
||||||
|
@ -96,6 +97,7 @@ func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
if gin_mode == debugCode {
|
if gin_mode == debugCode {
|
||||||
|
render.HTMLDebug.AddFiles(files...)
|
||||||
engine.HTMLRender = render.HTMLDebug
|
engine.HTMLRender = render.HTMLDebug
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.ParseFiles(files...))
|
templ := template.Must(template.ParseFiles(files...))
|
||||||
|
|
|
@ -30,7 +30,10 @@ type (
|
||||||
redirectRender struct{}
|
redirectRender struct{}
|
||||||
|
|
||||||
// Redirects
|
// Redirects
|
||||||
htmlDebugRender struct{}
|
htmlDebugRender struct {
|
||||||
|
files []string
|
||||||
|
globs []string
|
||||||
|
}
|
||||||
|
|
||||||
// form binding
|
// form binding
|
||||||
HTMLRender struct {
|
HTMLRender struct {
|
||||||
|
@ -43,7 +46,7 @@ var (
|
||||||
XML = xmlRender{}
|
XML = xmlRender{}
|
||||||
Plain = plainRender{}
|
Plain = plainRender{}
|
||||||
Redirect = redirectRender{}
|
Redirect = redirectRender{}
|
||||||
HTMLDebug = htmlDebugRender{}
|
HTMLDebug = &htmlDebugRender{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
||||||
|
@ -82,14 +85,33 @@ func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ htmlDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
func (r *htmlDebugRender) AddGlob(pattern string) {
|
||||||
|
r.globs = append(r.globs, pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *htmlDebugRender) AddFiles(files ...string) {
|
||||||
|
r.files = append(r.files, files...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *htmlDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
||||||
writeHeader(w, code, "text/html")
|
writeHeader(w, code, "text/html")
|
||||||
file := data[0].(string)
|
file := data[0].(string)
|
||||||
obj := data[1]
|
obj := data[1]
|
||||||
t, err := template.ParseFiles(file)
|
|
||||||
if err != nil {
|
t := template.New("")
|
||||||
return err
|
|
||||||
|
if len(r.files) > 0 {
|
||||||
|
if _, err := t.ParseFiles(r.files...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, glob := range r.globs {
|
||||||
|
if _, err := t.ParseGlob(glob); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return t.ExecuteTemplate(w, file, obj)
|
return t.ExecuteTemplate(w, file, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue