Fix for #119. gin.LoadHTML* incorrectly works in debug mode.

This commit is contained in:
Ludwig Valda Vasquez 2014-09-17 07:33:13 +04:00
parent 0808f8a824
commit 97ae4a6b65
2 changed files with 30 additions and 6 deletions

2
gin.go
View File

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

View File

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