-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
63 lines (56 loc) · 1.17 KB
/
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
54
55
56
57
58
59
60
61
62
63
package rtfblog
import (
"errors"
"html/template"
"path/filepath"
"sync"
)
const (
tmplDir = "tmpl"
)
type tmplMap map[string]interface{}
var (
cachedTemplates = map[string]*template.Template{}
cachedMutex sync.Mutex
funcs = template.FuncMap{
"dict": dict,
}
)
func dict(values ...interface{}) (tmplMap, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(tmplMap, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
func addTemplateFunc(name string, f interface{}) {
funcs[name] = f
}
func tmpl(c *Context, name string) *template.Template {
cachedMutex.Lock()
defer cachedMutex.Unlock()
if t, ok := cachedTemplates[name]; ok {
return t
}
t := template.New("base.html").Funcs(funcs)
for _, s := range []string{
"base.html",
"sidebar.html",
"post-title.html",
"header.html",
"author.html",
"captcha.html",
name,
} {
t = template.Must(t.Parse(string(c.assets.MustLoad(filepath.Join(tmplDir, s)))))
}
cachedTemplates[name] = t
return t
}