From 97ae4a6b654f7ce2a2b77b3905ffe16f0b6b2652 Mon Sep 17 00:00:00 2001 From: Ludwig Valda Vasquez Date: Wed, 17 Sep 2014 07:33:13 +0400 Subject: [PATCH 1/2] Fix for #119. gin.LoadHTML* incorrectly works in debug mode. --- gin.go | 2 ++ render/render.go | 34 ++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/gin.go b/gin.go index 45b3807..1ff2acf 100644 --- a/gin.go +++ b/gin.go @@ -87,6 +87,7 @@ func Default() *Engine { func (engine *Engine) LoadHTMLGlob(pattern string) { if gin_mode == debugCode { + render.HTMLDebug.AddGlob(pattern) engine.HTMLRender = render.HTMLDebug } else { templ := template.Must(template.ParseGlob(pattern)) @@ -96,6 +97,7 @@ func (engine *Engine) LoadHTMLGlob(pattern string) { func (engine *Engine) LoadHTMLFiles(files ...string) { if gin_mode == debugCode { + render.HTMLDebug.AddFiles(files...) engine.HTMLRender = render.HTMLDebug } else { templ := template.Must(template.ParseFiles(files...)) diff --git a/render/render.go b/render/render.go index 699b4e9..21b6160 100644 --- a/render/render.go +++ b/render/render.go @@ -30,7 +30,10 @@ type ( redirectRender struct{} // Redirects - htmlDebugRender struct{} + htmlDebugRender struct { + files []string + globs []string + } // form binding HTMLRender struct { @@ -43,7 +46,7 @@ var ( XML = xmlRender{} Plain = plainRender{} Redirect = redirectRender{} - HTMLDebug = htmlDebugRender{} + HTMLDebug = &htmlDebugRender{} ) 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 } -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") file := data[0].(string) obj := data[1] - t, err := template.ParseFiles(file) - if err != nil { - return err + + t := template.New("") + + 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) } From e5a7bcd6df90bb290bc5b802ba1e43388db163ac Mon Sep 17 00:00:00 2001 From: Ludwig Valda Vasquez Date: Wed, 17 Sep 2014 07:44:42 +0400 Subject: [PATCH 2/2] fix typo in PR for #119 --- render/render.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render/render.go b/render/render.go index 21b6160..a81fffe 100644 --- a/render/render.go +++ b/render/render.go @@ -107,7 +107,7 @@ func (r *htmlDebugRender) Render(w http.ResponseWriter, code int, data ...interf } for _, glob := range r.globs { - if _, err = t.ParseGlob(glob); err != nil { + if _, err := t.ParseGlob(glob); err != nil { return err } }