-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
128 lines (125 loc) · 2.92 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
package templateManager
import (
"fmt"
"io"
TT "text/template"
HT "html/template"
)
func NewTemplate(typ string, name string) *Template {
if typ == "html" {
return NewHtmlTemplate(name)
}
return NewTextTemplate(name)
}
func NewTextTemplate(name string) *Template {
template := &Template{ nil, nil }
template.InitText(name)
return template
}
func NewHtmlTemplate(name string) *Template {
template := &Template{ nil, nil }
template.InitHtml(name)
return template
}
/*
Allows the use of either HTML or Text template packages
*/
type Template struct {
html *HT.Template
text *TT.Template
}
func (t *Template) InitText(name string) *TT.Template {
t.text = TT.New(name)
t.html = nil
return t.text
}
func (t *Template) InitHtml(name string) *HT.Template {
t.html = HT.New(name)
t.text = nil
return t.html
}
func (t *Template) Delims(left string, right string) *Template {
if t.Type() == "text" {
t.text.Delims(left, right)
} else if t.Type() == "html" {
t.html.Delims(left, right)
}
return t
}
func (t *Template) Execute(wr io.Writer, data any) error {
if t.Type() == "text" {
return t.text.Execute(wr, data)
} else if t.Type() == "html" {
return t.html.Execute(wr, data)
}
return fmt.Errorf("templateManager Template not loaded correctly")
}
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error {
if t.Type() == "text" {
return t.text.ExecuteTemplate(wr, name, data)
} else if t.Type() == "html" {
return t.html.ExecuteTemplate(wr, name, data)
}
return fmt.Errorf("templateManager Template not loaded correctly")
}
func (t *Template) Funcs(funcs map[string]any) *Template {
if t.Type() == "text" {
t.text.Funcs(funcs)
} else if t.Type() == "html" {
t.html.Funcs(funcs)
}
return t
}
func (t *Template) Lookup(name string) *Template {
if t.Type() == "text" {
template := t.text.Lookup(name)
if template == nil {
return nil
}
return &Template{ nil, template }
} else if t.Type() == "html" {
template := t.html.Lookup(name)
if template == nil {
return nil
}
return &Template{ template, nil }
}
return nil
}
func (t *Template) NewSubTemplate(name string) *Template {
if t.Type() == "text" {
template := t.text.New(name)
return &Template{ nil, template }
} else if t.Type() == "html" {
template := t.html.New(name)
return &Template{ template, nil }
}
return t
}
func (t *Template) Option(opt ...string) *Template {
if t.Type() == "text" {
t.text.Option(opt...)
} else if t.Type() == "html" {
t.html.Option(opt...)
}
return t
}
func (t *Template) Parse(text string) (*Template, error) {
var err error
if t.Type() == "text" {
_, err = t.text.Parse(text)
} else if t.Type() == "html" {
_, err = t.html.Parse(text)
} else {
err = fmt.Errorf("templateManager Template not loaded correctly")
}
return t, err
}
func (t *Template) Type() string {
if t.text == nil && t.html == nil {
return "invalid"
} else if t.text == nil {
return "html"
}
return "text"
}