From 378610b3b215bc8d07c4c4b6dc42c59a8fe20b1a Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 20 Aug 2014 18:14:10 +0200 Subject: [PATCH] HTML template debug mode. - theHTML templates are reloaded each time --- gin.go | 8 ++++++-- render/render.go | 33 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/gin.go b/gin.go index c1adbb49..8b6d17d5 100644 --- a/gin.go +++ b/gin.go @@ -92,8 +92,12 @@ func (engine *Engine) LoadHTMLFiles(files ...string) { } func (engine *Engine) SetHTMLTemplate(templ *template.Template) { - engine.HTMLRender = render.HTMLRender{ - Template: templ, + if gin_mode == debugCode { + engine.HTMLRender = render.HTMLDebug + } else { + engine.HTMLRender = render.HTMLRender{ + Template: templ, + } } } diff --git a/render/render.go b/render/render.go index 207e7a52..57a59fe1 100644 --- a/render/render.go +++ b/render/render.go @@ -25,6 +25,9 @@ type ( // Redirects redirectRender struct{} + // Redirects + htmlDebugRender struct{} + // form binding HTMLRender struct { Template *template.Template @@ -32,10 +35,11 @@ type ( ) var ( - JSON = jsonRender{} - XML = xmlRender{} - Plain = plainRender{} - Redirect = redirectRender{} + JSON = jsonRender{} + XML = xmlRender{} + Plain = plainRender{} + Redirect = redirectRender{} + HTMLDebug = htmlDebugRender{} ) func writeHeader(w http.ResponseWriter, code int, contentType string) { @@ -61,13 +65,6 @@ func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) return encoder.Encode(data[0]) } -func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { - writeHeader(w, code, "text/html") - file := data[0].(string) - obj := data[1] - return html.Template.ExecuteTemplate(w, file, obj) -} - func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { writeHeader(w, code, "text/plain") format := data[0].(string) @@ -80,3 +77,17 @@ func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{} } return err } + +func (_ htmlDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { + writeHeader(w, code, "text/html") + file := data[0].(string) + obj := data[1] + return template.New(file).Execute(w, obj) +} + +func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { + writeHeader(w, code, "text/html") + file := data[0].(string) + obj := data[1] + return html.Template.ExecuteTemplate(w, file, obj) +}