forked from uadmin/uadmin
/
render_html.go
78 lines (70 loc) · 2.08 KB
/
render_html.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package uadmin
import (
"fmt"
"html"
"html/template"
"net/http"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
)
// RenderHTML creates a new template and applies a parsed template to the specified
// data object. For function, Tf is available by default and if you want to add functions
//to your template, just add them to funcs which will add them to the template with their
// original function names. If you added anonymous functions, they will be available in your
// templates as func1, func2 ...etc.
func RenderHTML(w http.ResponseWriter, r *http.Request, path string, data interface{}, funcs ...interface{}) {
var err error
var funcVal reflect.Value
var funcName string
funcMap := template.FuncMap{
"Tf": Tf,
"CSRF": func() string {
return getSession(r)
},
}
for i := range funcs {
funcVal = reflect.ValueOf(funcs[i])
if funcVal.Type().Kind() != reflect.Func {
Trail(WARNING, "Interface passed to RenderHTML in funcs parameter should only be a function. Got (%s) in position %d", funcVal.Type().Kind(), i)
continue
}
funcName = runtime.FuncForPC(funcVal.Pointer()).Name()
funcName = funcName[strings.LastIndex(funcName, ".")+1:]
funcMap[funcName] = funcs[i]
}
// Check for ABTesting cookie
if cookie, err := r.Cookie("abt"); err != nil || cookie == nil {
now := time.Now().AddDate(0, 0, 1)
cookie = &http.Cookie{
Name: "abt",
Value: fmt.Sprint(now.Second()),
Path: "/",
Expires: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()),
}
http.SetCookie(w, cookie)
}
t := template.New("").Funcs(funcMap)
t, err = t.ParseFiles(path)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, html.EscapeString(err.Error()))
Trail(ERROR, "RenderHTML unable to parse %s. %s", path, err)
return
}
path = filepath.Base(path)
err = t.ExecuteTemplate(w, path, data)
if err != nil {
ignoredErrors := []string{
"write tcp",
}
for i := range ignoredErrors {
if strings.HasPrefix(err.Error(), ignoredErrors[i]) {
return
}
}
Trail(ERROR, "Unable to render html template file (%s). %s", path, err)
}
}