2015-05-07 10:44:52 +00:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
HTMLRender struct {
|
|
|
|
Template *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlPlainRender struct{}
|
|
|
|
|
|
|
|
HTMLDebugRender struct {
|
|
|
|
Files []string
|
|
|
|
Glob string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
2015-05-10 23:02:17 +00:00
|
|
|
writeHeader(w, code, "text/html; charset=utf-8")
|
2015-05-07 10:44:52 +00:00
|
|
|
file := data[0].(string)
|
|
|
|
args := data[1]
|
|
|
|
return html.Template.ExecuteTemplate(w, file, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
2015-05-10 23:02:17 +00:00
|
|
|
writeHeader(w, code, "text/html; charset=utf-8")
|
2015-05-07 10:44:52 +00:00
|
|
|
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) {
|
|
|
|
if len(r.Files) > 0 {
|
|
|
|
return template.ParseFiles(r.Files...)
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
format := data[0].(string)
|
|
|
|
values := data[1].([]interface{})
|
|
|
|
WriteHTMLString(w, code, format, values)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteHTMLString(w http.ResponseWriter, code int, format string, values []interface{}) {
|
2015-05-10 23:02:17 +00:00
|
|
|
writeHeader(w, code, "text/html; charset=utf-8")
|
2015-05-07 10:44:52 +00:00
|
|
|
if len(values) > 0 {
|
|
|
|
fmt.Fprintf(w, format, values...)
|
|
|
|
} else {
|
|
|
|
w.Write([]byte(format))
|
|
|
|
}
|
|
|
|
}
|