Fixes html debug mode

This commit is contained in:
Manu Mtz-Almeida 2014-08-21 01:04:35 +02:00
parent 94f2f3f7eb
commit 46225ea53a
2 changed files with 22 additions and 14 deletions

26
gin.go
View File

@ -82,24 +82,28 @@ func Default() *Engine {
} }
func (engine *Engine) LoadHTMLGlob(pattern string) { func (engine *Engine) LoadHTMLGlob(pattern string) {
templ := template.Must(template.ParseGlob(pattern))
engine.SetHTMLTemplate(templ)
}
func (engine *Engine) LoadHTMLFiles(files ...string) {
templ := template.Must(template.ParseFiles(files...))
engine.SetHTMLTemplate(templ)
}
func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
if gin_mode == debugCode { if gin_mode == debugCode {
engine.HTMLRender = render.HTMLDebug engine.HTMLRender = render.HTMLDebug
} else { } else {
templ := template.Must(template.ParseGlob(pattern))
engine.SetHTMLTemplate(templ)
}
}
func (engine *Engine) LoadHTMLFiles(files ...string) {
if gin_mode == debugCode {
engine.HTMLRender = render.HTMLDebug
} else {
templ := template.Must(template.ParseFiles(files...))
engine.SetHTMLTemplate(templ)
}
}
func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
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

@ -82,7 +82,11 @@ func (_ htmlDebugRender) Render(w http.ResponseWriter, code int, data ...interfa
writeHeader(w, code, "text/html") writeHeader(w, code, "text/html")
file := data[0].(string) file := data[0].(string)
obj := data[1] obj := data[1]
return template.New(file).Execute(w, obj) t, err := template.ParseFiles(file)
if err != nil {
return err
}
return t.ExecuteTemplate(w, file, obj)
} }
func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {