forked from haming123/wego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_templates.go
53 lines (45 loc) · 1.05 KB
/
web_templates.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
package wego
import (
"fmt"
"html/template"
"path/filepath"
"sync"
)
type WebTemplates struct {
mux sync.Mutex
tpl_map map[string]*template.Template
delim_left string
delim_right string
}
func (this *WebTemplates) Init() {
this.tpl_map = make(map[string]*template.Template)
this.delim_left = "{{"
this.delim_right = "}}"
}
func (this *WebTemplates) SetDelim(left, right string){
this.delim_left = left
this.delim_right = right
}
func (this *WebTemplates) getTemplate(filenames ...string) (*template.Template, error) {
this.mux.Lock()
defer this.mux.Unlock()
if len(filenames) == 0 {
return nil, fmt.Errorf("must have one file name")
}
key := ""
for _, item := range filenames {
key += item
}
tpl, ok := this.tpl_map[key]
if ok {
//fmt.Println("use template cache")
return tpl, nil
}
//fmt.Println("load template")
name := filepath.Base(filenames[0])
t, err := template.New(name).Delims(this.delim_left, this.delim_right).Funcs(tmplFuncMap).ParseFiles(filenames...)
if err == nil {
this.tpl_map[key] = t
}
return t, err
}