forked from wish/eventmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpl.go
76 lines (64 loc) · 1.86 KB
/
tmpl.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
package eventmaster
import (
"fmt"
"html/template"
"path/filepath"
"sync"
)
// TemplateGetter defines what needs to be implemented to be able to fetch
// templates for use by a Site.
type TemplateGetter interface {
Get(name string) (*template.Template, error)
}
// Disk keeps track of the location of the root directory of the template
// directory.
type Disk struct {
Root string
}
// Get attempts to merge the requested name+.html with base.html found at root.
func (d Disk) Get(name string) (*template.Template, error) {
p := filepath.Join(d.Root, name)
return template.New("main.html").Funcs(funcMap).ParseFiles(p, filepath.Join(d.Root, "main.html"))
}
// AssetTemplate pulls templates from an assetfs.
type AssetTemplate struct {
sync.RWMutex
Asset func(name string) ([]byte, error)
cache map[string]*template.Template
}
// NewAssetTemplate returns a ready-to-use AssetTemplate.
func NewAssetTemplate(asset func(string) ([]byte, error)) *AssetTemplate {
return &AssetTemplate{
Asset: asset,
cache: map[string]*template.Template{},
}
}
// Get attempts to merge the requested name+.html with base.html found at root.
func (at *AssetTemplate) Get(name string) (*template.Template, error) {
at.RLock()
if t, ok := at.cache[name]; ok {
at.RUnlock()
return t, nil
}
at.RUnlock()
base, err := at.Asset("templates/main.html")
if err != nil {
return nil, fmt.Errorf("could not get base contents: %v", err)
}
page, err := at.Asset(fmt.Sprintf("templates/%s", name))
if err != nil {
return nil, fmt.Errorf("could not get base contents: %v", err)
}
t, err := template.New(name).Funcs(funcMap).Parse(string(base))
if err != nil {
return t, fmt.Errorf("parsing base: %v", err)
}
t, err = t.Parse(string(page))
if err != nil {
return t, fmt.Errorf("parsing specific page %s: %v", name, err)
}
at.Lock()
at.cache[name] = t
at.Unlock()
return t, nil
}