-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_template.go
54 lines (50 loc) · 1.09 KB
/
router_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
package cotton
import (
"io/ioutil"
"path/filepath"
"strings"
"text/template"
)
func getTplFiles(dir string, ext string) (list []string, err error) {
files, err := ioutil.ReadDir(dir)
for _, f := range files {
if strings.HasPrefix(f.Name(), ".") {
continue
}
path := filepath.Join(dir, f.Name())
if f.IsDir() {
listSub, e := getTplFiles(path, ext)
if e != nil {
err = e
return
}
list = append(listSub, list...)
} else if filepath.Ext(f.Name()) == ext {
list = append(list, path)
}
}
return
}
// LoadTemplates load template files
//
// funcs is functions register to template
// example:
// router.LoadTemplates(root, map[string]interface{}{
// "md5": func(str string) string {
// return str + "_md5"
// },
// })
func (router *Router) LoadTemplates(tplRoot string, funcs map[string]interface{}) {
if router.globalTemplate == nil {
tpl := template.New("global")
if funcs != nil && len(funcs) > 0 {
tpl.Funcs(funcs)
}
list, e := getTplFiles(tplRoot, ".html")
if e != nil {
panic(e)
}
tpl.ParseFiles(list...)
router.globalTemplate = tpl
}
}