-
Notifications
You must be signed in to change notification settings - Fork 15
/
template.go
161 lines (136 loc) · 3.7 KB
/
template.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package render
import (
"bytes"
"fmt"
"html/template"
"net/http"
"path/filepath"
"strings"
)
// Template template struct
type Template struct {
render *Render
layout string
usingDefaultLayout bool
funcMap template.FuncMap
}
// FuncMap get func maps from tmpl
func (tmpl *Template) funcMapMaker(req *http.Request, writer http.ResponseWriter) template.FuncMap {
var funcMap = template.FuncMap{}
for key, fc := range tmpl.render.funcMaps {
funcMap[key] = fc
}
if tmpl.render.Config.FuncMapMaker != nil {
for key, fc := range tmpl.render.Config.FuncMapMaker(tmpl.render, req, writer) {
funcMap[key] = fc
}
}
for key, fc := range tmpl.funcMap {
funcMap[key] = fc
}
return funcMap
}
// Funcs register Funcs for tmpl
func (tmpl *Template) Funcs(funcMap template.FuncMap) *Template {
tmpl.funcMap = funcMap
return tmpl
}
// Render render tmpl
func (tmpl *Template) Render(templateName string, obj interface{}, request *http.Request, writer http.ResponseWriter) (template.HTML, error) {
var (
content []byte
t *template.Template
err error
funcMap = tmpl.funcMapMaker(request, writer)
render = func(name string, objs ...interface{}) (template.HTML, error) {
var (
err error
renderObj interface{}
renderContent []byte
)
if len(objs) == 0 {
// default obj
renderObj = obj
} else {
// overwrite obj
for _, o := range objs {
renderObj = o
break
}
}
if renderContent, err = tmpl.findTemplate(name); err == nil {
var partialTemplate *template.Template
result := bytes.NewBufferString("")
if partialTemplate, err = template.New(filepath.Base(name)).Funcs(funcMap).Parse(string(renderContent)); err == nil {
if err = partialTemplate.Execute(result, renderObj); err == nil {
return template.HTML(result.String()), err
}
}
} else {
err = fmt.Errorf("failed to find template: %v", name)
}
if err != nil {
fmt.Println(err)
}
return "", err
}
)
// funcMaps
funcMap["render"] = render
funcMap["yield"] = func() (template.HTML, error) { return render(templateName) }
layout := tmpl.layout
usingDefaultLayout := false
if layout == "" && tmpl.usingDefaultLayout {
usingDefaultLayout = true
layout = tmpl.render.DefaultLayout
}
var tpl bytes.Buffer
if layout != "" {
content, err = tmpl.findTemplate(filepath.Join("layouts", layout))
if err == nil {
t, err = template.New("").Funcs(funcMap).Parse(string(content))
if err != nil {
goto OnError
}
err = t.Execute(&tpl, obj)
if err != nil {
goto OnError
}
return template.HTML(tpl.String()), nil
} else if !usingDefaultLayout {
goto OnError
}
}
content, err = tmpl.findTemplate(templateName)
if err != nil {
goto OnError
}
t, err = template.New("").Funcs(funcMap).Parse(string(content))
if err != nil {
goto OnError
}
err = t.Execute(&tpl, obj)
if err != nil {
goto OnError
}
return template.HTML(tpl.String()), nil
OnError:
err = fmt.Errorf("Failed to render page '%s' with template '%v.tmpl', got error: %v", templateName, filepath.Join("layouts", tmpl.layout), err)
fmt.Println(err)
// Display error in page directly
return template.HTML(err.Error()), nil
}
// Execute execute tmpl
func (tmpl *Template) Execute(templateName string, obj interface{}, req *http.Request, w http.ResponseWriter) error {
result, err := tmpl.Render(templateName, obj, req, w)
if err == nil {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "text/html")
}
_, err = w.Write([]byte(result))
}
return err
}
func (tmpl *Template) findTemplate(name string) ([]byte, error) {
return tmpl.render.Asset(strings.TrimSpace(name) + ".tmpl")
}