gin/render/html_debug.go

39 lines
878 B
Go
Raw Normal View History

// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package render
import (
"errors"
"html/template"
"net/http"
)
type HTMLDebugRender struct {
2015-03-31 18:39:30 +03:00
Files []string
Glob string
}
func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
WriteHeader(w, code, "text/html")
file := data[0].(string)
obj := data[1]
if t, err := r.loadTemplate(); err == nil {
return t.ExecuteTemplate(w, file, obj)
} else {
return err
}
}
func (r *HTMLDebugRender) loadTemplate() (*template.Template, error) {
2015-03-31 18:39:30 +03:00
if len(r.Files) > 0 {
return template.ParseFiles(r.Files...)
}
2015-03-31 18:39:30 +03:00
if len(r.Glob) > 0 {
return template.ParseGlob(r.Glob)
}
return nil, errors.New("the HTML debug render was created without files or glob pattern")
}