HTML template debug mode.

- theHTML templates are reloaded each time
This commit is contained in:
Manu Mtz-Almeida 2014-08-20 18:14:10 +02:00
parent 7963d018a8
commit 378610b3b2
2 changed files with 28 additions and 13 deletions

8
gin.go
View File

@ -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,
}
}
}

View File

@ -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)
}