-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
126 lines (106 loc) · 2.6 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
package ela
import (
"fmt"
"github.com/gogather/com"
//"github.com/gogather/com/log"
"html/template"
"io/ioutil"
"sort"
"strings"
)
var (
templates map[string]string
templatesName []string
templatefolder string
templatesFilterOut []string
funcMap template.FuncMap
)
func init() {
templates = make(map[string]string)
templatefolder = config.GetStringDefault("_", "template", "web/view")
listFile(templatefolder)
// initialize template functions
funcMap = template.FuncMap{}
AddTemplateFunc("tplFunc", tplFunc)
}
func reloadTemplate() {
listFile(templatefolder)
}
func addFilterOut(filename string) {
templatesFilterOut = append(templatesFilterOut, filename)
}
func notInFilterOut(filename string) bool {
for i := 0; i < len(templatesFilterOut); i++ {
if templatesFilterOut[i] == filename {
return false
}
}
return true
}
func listFile(dir string) {
addFilterOut(".DS_Store")
files, _ := ioutil.ReadDir(dir)
for _, file := range files {
subfile := dir + "/" + file.Name()
if file.IsDir() {
listFile(subfile)
} else if notInFilterOut(file.Name()) {
content, err := com.ReadFileString(subfile)
if err != nil {
templates[subfile] = ""
} else {
content = "{{define \"" + subfile + "\"}}" + content + "{{end}}"
content = lefTplDir(content, templatefolder)
subfile = lefTplDir(subfile, templatefolder)
templates[subfile] = content
}
}
}
// get template name list
getTemplateNames()
}
func getTemplateNames() {
templatesName = make([]string, 0, len(templates))
for k := range templates {
templatesName = append(templatesName, k)
}
}
func lefTplDir(dir string, tplDir string) string {
return strings.Replace(dir, tplDir+"/", "", 1)
}
func parseFiles(filenames ...string) (*template.Template, error) {
var t *template.Template = nil
var err error = nil
if len(filenames) == 0 {
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
}
sort.Strings(filenames)
for _, filename := range filenames {
if t == nil {
t = template.New(filename)
}
if filename != t.Name() {
t = t.New(filename)
}
_, err = t.Funcs(funcMap).Parse(templates[filename])
// anyone template syntax error throw panic
if err != nil {
panic(err)
}
}
return t, err
}
func SetTemplateDir(dir string) {
exist := com.FileExist(dir)
if exist {
templatefolder = dir
}
}
// add template function definition
func AddTemplateFunc(functionName string, function interface{}) {
funcMap[functionName] = function
}
// test template function define
func tplFunc(test string) string {
return "template function, " + test
}