forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_debug.go
42 lines (36 loc) · 885 Bytes
/
html_debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 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 (
"html/template"
"net/http"
)
type HTMLDebugRender struct {
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.newTemplate(); err == nil {
return t.ExecuteTemplate(w, file, obj)
} else {
return err
}
}
func (r *HTMLDebugRender) newTemplate() (*template.Template, error) {
t := template.New("")
if len(r.Files) > 0 {
if _, err := t.ParseFiles(r.Files...); err != nil {
return nil, err
}
}
if len(r.Glob) > 0 {
if _, err := t.ParseGlob(r.Glob); err != nil {
return nil, err
}
}
return t, nil
}