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

4
gin.go
View File

@ -92,10 +92,14 @@ func (engine *Engine) LoadHTMLFiles(files ...string) {
} }
func (engine *Engine) SetHTMLTemplate(templ *template.Template) { func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
if gin_mode == debugCode {
engine.HTMLRender = render.HTMLDebug
} else {
engine.HTMLRender = render.HTMLRender{ engine.HTMLRender = render.HTMLRender{
Template: templ, Template: templ,
} }
} }
}
// Adds handlers for NoRoute. It return a 404 code by default. // Adds handlers for NoRoute. It return a 404 code by default.
func (engine *Engine) NoRoute(handlers ...HandlerFunc) { func (engine *Engine) NoRoute(handlers ...HandlerFunc) {

View File

@ -25,6 +25,9 @@ type (
// Redirects // Redirects
redirectRender struct{} redirectRender struct{}
// Redirects
htmlDebugRender struct{}
// form binding // form binding
HTMLRender struct { HTMLRender struct {
Template *template.Template Template *template.Template
@ -36,6 +39,7 @@ var (
XML = xmlRender{} XML = xmlRender{}
Plain = plainRender{} Plain = plainRender{}
Redirect = redirectRender{} Redirect = redirectRender{}
HTMLDebug = htmlDebugRender{}
) )
func writeHeader(w http.ResponseWriter, code int, contentType string) { 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]) 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 { func (_ plainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
writeHeader(w, code, "text/plain") writeHeader(w, code, "text/plain")
format := data[0].(string) format := data[0].(string)
@ -80,3 +77,17 @@ 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 {
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)
}