2015-03-31 18:36:13 +03:00
|
|
|
// 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 (
|
2015-05-05 16:06:38 +03:00
|
|
|
"errors"
|
2015-03-31 18:36:13 +03:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HTMLDebugRender struct {
|
2015-03-31 18:39:30 +03:00
|
|
|
Files []string
|
|
|
|
Glob string
|
2015-03-31 18:36:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
WriteHeader(w, code, "text/html")
|
|
|
|
file := data[0].(string)
|
|
|
|
obj := data[1]
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
if t, err := r.loadTemplate(); err == nil {
|
2015-03-31 18:36:13 +03:00
|
|
|
return t.ExecuteTemplate(w, file, obj)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func (r *HTMLDebugRender) loadTemplate() (*template.Template, error) {
|
2015-03-31 18:39:30 +03:00
|
|
|
if len(r.Files) > 0 {
|
2015-05-05 16:06:38 +03:00
|
|
|
return template.ParseFiles(r.Files...)
|
2015-03-31 18:36:13 +03:00
|
|
|
}
|
2015-03-31 18:39:30 +03:00
|
|
|
if len(r.Glob) > 0 {
|
2015-05-05 16:06:38 +03:00
|
|
|
return template.ParseGlob(r.Glob)
|
2015-03-31 18:36:13 +03:00
|
|
|
}
|
2015-05-05 16:06:38 +03:00
|
|
|
return nil, errors.New("the HTML debug render was created without files or glob pattern")
|
2015-03-31 18:36:13 +03:00
|
|
|
}
|