-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
68 lines (53 loc) · 1.51 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
package main
import (
"io"
htmltemplate "html/template"
texttemplate "text/template"
)
type template interface {
New(string) template
Parse(string) (template, error)
ExecuteTemplate(io.Writer, string, interface{}) error
}
type textTemplate struct {
t *texttemplate.Template
}
type htmlTemplate struct {
t *htmltemplate.Template
}
var htmlOrText = map[bool]func(n, l, r string, f map[string]interface{}) template{
false: newtext,
true: newhtml,
}
func newtext(name, left, right string, funcs map[string]interface{}) template {
return &textTemplate{texttemplate.New(name).Delims(left, right).Funcs(funcs)}
}
func newhtml(name, left, right string, funcs map[string]interface{}) template {
return &htmlTemplate{htmltemplate.New(name).Delims(left, right).Funcs(funcs)}
}
func (t *textTemplate) New(nm string) template {
return &textTemplate{t.t.New(nm)}
}
func (t *textTemplate) Parse(s string) (template, error) {
x, err := t.t.Parse(s)
if err != nil {
return nil, err
}
return &textTemplate{x}, nil
}
func (t *textTemplate) ExecuteTemplate(w io.Writer, which string, data interface{}) error {
return t.t.ExecuteTemplate(w, which, data)
}
func (t *htmlTemplate) New(nm string) template {
return &htmlTemplate{t.t.New(nm)}
}
func (t *htmlTemplate) Parse(s string) (template, error) {
x, err := t.t.Parse(s)
if err != nil {
return nil, err
}
return &htmlTemplate{x}, nil
}
func (t *htmlTemplate) ExecuteTemplate(w io.Writer, which string, data interface{}) error {
return t.t.ExecuteTemplate(w, which, data)
}