-
Notifications
You must be signed in to change notification settings - Fork 28
/
template.go
97 lines (91 loc) · 2.05 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
package template
import (
"bytes"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/gk/utils"
"path/filepath"
"reflect"
"strings"
"text/template"
)
var engine Engine
type Engine interface {
init()
Execute(name string, model interface{}) (string, error)
ExecuteString(data string, model interface{}) (string, error)
}
type DefaultEngine struct {
t *template.Template
}
func funcMap() template.FuncMap {
return template.FuncMap{
"last": func(x int, a interface{}) bool {
return x == reflect.ValueOf(a).Len()-1
},
"toSnakeCase": func(s string) string {
return utils.ToLowerSnakeCase(s)
},
"toUpperFirstCamelCase": func(s string) string {
return utils.ToUpperFirstCamelCase(s)
},
"toUpperFirst": func(s string) string {
return utils.ToUpperFirst(s)
},
"fileSeparator": func() string {
if filepath.Separator == '\\' {
return "\\\\"
}
return string(filepath.Separator)
},
"toCamelCase": func(s string) string {
return utils.ToCamelCase(s)
},
}
}
func NewEngine() Engine {
if engine == nil {
engine = &DefaultEngine{}
engine.init()
}
return engine
}
func (e *DefaultEngine) init() {
e.t = template.New("default")
e.t.Funcs(funcMap())
for n, v := range _bintree.Children["tmpl"].Children["partials"].Children {
a, _ := v.Func()
_, err := e.t.Parse(
fmt.Sprintf(
"{{define \"%s\"}} %s {{end}}",
strings.Replace(n, ".tmpl", "", 1),
string(a.bytes),
),
)
if err != nil {
logrus.Panic(err)
}
}
}
func (e *DefaultEngine) Execute(name string, model interface{}) (string, error) {
d, err := Asset(fmt.Sprintf("tmpl/%s.tmpl", name))
if err != nil {
logrus.Panic(err)
}
tmp, err := e.t.Parse(string(d))
if err != nil {
logrus.Panic(err)
}
ret := bytes.NewBufferString("")
err = tmp.Execute(ret, model)
return ret.String(), err
}
func (e *DefaultEngine) ExecuteString(data string, model interface{}) (string, error) {
tmp, err := e.t.Parse(data)
if err != nil {
logrus.Panic(err)
}
ret := bytes.NewBufferString("")
err = tmp.Execute(ret, model)
return ret.String(), err
}