-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
264 lines (213 loc) · 5.08 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package template
import (
"fmt"
"golanger.com/log"
"io"
"io/ioutil"
"path/filepath"
"text/template"
"text/template/parse"
)
type FuncMap map[string]interface{}
type Template struct {
text *template.Template
*nameSpace
}
func Must(t *Template, err error) *Template {
if err != nil {
log.Panic("<Must> Name: ", t.Name(), ", error:", err)
return nil
}
return t
}
func Test(t *Template, err error) *Template {
if err != nil {
log.Error("<Test> Name: ", t.Name(), ", error:", err)
}
return t
}
func New(name string) *Template {
tmpl := &Template{
template.New(name),
&nameSpace{
tmpls: make(map[string]*Template),
},
}
tmpl.nameSpace.Insert(name, tmpl)
return tmpl
}
//修正标准库的parseFiles在解析文件的时候,如果不同路径,同名也会报错的bug
func parseFiles(t *Template, filenames ...string) (*Template, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("template: no files named in call to ParseFiles")
}
for _, filename := range filenames {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
s := string(b)
name := filepath.Clean(filename)
var tmpl *Template
if t == nil {
t = New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
}
return t, nil
}
func parseGlob(t *Template, pattern string) (*Template, error) {
filenames, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
if len(filenames) == 0 {
return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
}
return parseFiles(t, filenames...)
}
func ParseFiles(filenames ...string) (*Template, error) {
return parseFiles(nil, filenames...)
}
func ParseGlob(pattern string) (*Template, error) {
return parseGlob(nil, pattern)
}
func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
text, err := t.text.AddParseTree(name, tree)
if err != nil {
return nil, err
}
t.nameSpace.mu.RLock()
ret := &Template{
text,
t.nameSpace,
}
t.nameSpace.mu.RUnlock()
t.nameSpace.Upsert(name, ret)
return ret, nil
}
func (t *Template) Clone() (*Template, error) {
textClone, err := t.text.Clone()
if err != nil {
return nil, err
}
ret := &Template{
textClone,
&nameSpace{
tmpls: make(map[string]*Template),
},
}
for _, x := range textClone.Templates() {
name := x.Name()
if _, ok := t.nameSpace.get(name); !ok {
return nil, fmt.Errorf("html/template: cannot Clone %q after it has executed", t.Name())
}
if x.Tree != nil {
x.Tree = &parse.Tree{
Name: x.Tree.Name,
Root: x.Tree.Root.CopyList(),
}
}
t.nameSpace.mu.RLock()
ret.nameSpace.Upsert(name, &Template{
x,
ret.nameSpace,
})
t.nameSpace.mu.RUnlock()
}
return ret, nil
}
func (t *Template) Delims(left, right string) *Template {
t.text.Delims(left, right)
return t
}
func (t *Template) Execute(wr io.Writer, data interface{}) (err error) {
return t.text.Execute(wr, data)
}
func (t *Template) lookup(name string) (tmpl *Template, err error) {
tmpl, ok := t.nameSpace.get(name)
if !ok {
return nil, fmt.Errorf("template: %q is undefined", name)
} else {
t.nameSpace.mu.RLock()
defer t.nameSpace.mu.RUnlock()
if tmpl.text.Tree == nil || tmpl.text.Root == nil {
return nil, fmt.Errorf("template: %q is an incomplete template", name)
}
if t.text.Lookup(name) == nil {
log.Panic("template internal error: template escaping out of sync")
}
}
return tmpl, err
}
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
tmpl, err := t.lookup(name)
if err != nil {
return err
}
return tmpl.text.Execute(wr, data)
}
func (t *Template) Funcs(funcMap FuncMap) *Template {
t.text.Funcs(template.FuncMap(funcMap))
return t
}
func (t *Template) Name() string {
return t.text.Name()
}
func (t *Template) New(name string) *Template {
return t.new(name)
}
func (t *Template) new(name string) *Template {
t.nameSpace.mu.RLock()
tmpl := &Template{
t.text.New(name),
t.nameSpace,
}
t.nameSpace.mu.RUnlock()
tmpl.nameSpace.Upsert(name, tmpl)
return tmpl
}
func (t *Template) Templates() []*Template {
ns := t.nameSpace
// Return a slice so we don't expose the map.
m := make([]*Template, 0, ns.Length())
ns.mu.RLock()
for _, v := range ns.tmpls {
m = append(m, v)
}
ns.mu.RUnlock()
return m
}
func (t *Template) Parse(src string) (*Template, error) {
ret, err := t.text.Parse(src)
if err != nil {
return nil, err
}
// In general, all the named templates might have changed underfoot.
// Regardless, some new ones may have been defined.
// The template.Template set has been updated; update ours.
for _, v := range ret.Templates() {
name := v.Name()
tmpl, ok := t.nameSpace.get(name)
if !ok {
tmpl = t.new(name).Funcs(extFunc)
}
tmpl.text = v
}
return t, nil
}
func (t *Template) ParseGlob(pattern string) (*Template, error) {
return parseGlob(t, pattern)
}
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
return parseFiles(t, filenames...)
}