-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
62 lines (50 loc) · 1.25 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
package template
import (
"io"
"sync/atomic"
"github.com/valyala/fasttemplate"
)
var templateID = atomic.Int64{}
type Template struct {
id int
tmplStr string
tmpl *fasttemplate.Template
isDynamic bool
tagToIndex map[string]int
indexToTag map[string]string
}
func Parse(template string) *Template {
templateID.Add(1)
template = prepareDynamicValues(template)
template = minifyTemplate(template)
// template, tags := replaceTagsWithIndexes(template)
return &Template{
id: int(templateID.Load()),
tmplStr: template,
tmpl: fasttemplate.New(template, "~", "~"),
// isDynamic: len(tags) > 0,
// tagToIndex: tags,
// indexToTag: convTagToIndexToIndexToTag(tags),
}
}
func (t *Template) String() string {
return t.tmplStr
}
func (t *Template) HTML() string {
return ""
}
func (t *Template) ID() int {
return t.id
}
func (t *Template) IsDynamic() bool {
return t.isDynamic
}
func (t *Template) DynamicValues() map[string]int {
return t.tagToIndex
}
func (t *Template) Execute(w io.Writer, fn func(w io.Writer, tag string) (int, error)) (int64, error) {
return t.tmpl.ExecuteFunc(w, fn)
}
func (t *Template) ExecuteString(fn func(w io.Writer, tag string) (int, error)) string {
return t.tmpl.ExecuteFuncString(fn)
}