/
template.go
54 lines (44 loc) · 1.05 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
package sansmagic
import (
"mustache"
//"io/ioutil"
"path/filepath"
"os"
"log"
)
// ------------------------------------------------------------------
type TemplateMgr struct {
Templates map[string]*mustache.Template
}
func NewTemplateMgr(paths []string) *TemplateMgr {
tm := new(TemplateMgr)
tm.Templates = make(map[string]*mustache.Template)
errChan := make(chan os.Error)
for _, p := range paths {
filepath.Walk(p, tm, errChan)
}
return tm
}
func (tm TemplateMgr) Get(path string) *mustache.Template {
temp, ok := tm.Templates[path]
if !ok {
log.Fatal("Template: ", path, " not found")
}
return temp
}
func (tm *TemplateMgr) VisitDir(path string, f *os.FileInfo) bool {
// what is this supposed to do? read the go docs on this method.
return true
}
func (tm *TemplateMgr) VisitFile(path string, f *os.FileInfo) {
temp, err := mustache.ParseFile(path)
DieIf(err)
if path[0] == '.' {
return
}
if prev, ok := tm.Templates[path]; ok {
log.Print(f)
log.Fatal("overloading templates: ", prev)
}
tm.Templates[path] = temp
}