-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.go
62 lines (48 loc) · 1.23 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
55
56
57
58
59
60
61
62
package binhtml
import (
"html/template"
"path/filepath"
)
type AssetFunc func(string) ([]byte, error)
type AssetDirFunc func(string) ([]string, error)
type BinTemplate struct {
Asset AssetFunc
AssetDir AssetDirFunc
}
func New(a AssetFunc, b AssetDirFunc) *BinTemplate {
return &BinTemplate{Asset: a, AssetDir: b}
}
func (t *BinTemplate) LoadDirectory(directory string) (*template.Template, error) {
var tmpl *template.Template
return t.LoadDirectoryWithTemplate(tmpl, directory)
}
func (t *BinTemplate) LoadDirectoryWithTemplate(tmpl *template.Template, directory string) (*template.Template, error) {
files, err := t.AssetDir(directory)
if err != nil {
return tmpl, err
}
for _, filePath := range files {
contents, err := t.Asset(directory + "/" + filePath)
if err != nil {
return tmpl, err
}
name := filepath.Base(filePath)
if tmpl == nil {
tmpl = template.New(name)
}
if name != tmpl.Name() {
tmpl = tmpl.New(name)
}
if _, err = tmpl.Parse(string(contents)); err != nil {
return tmpl, err
}
}
return tmpl, nil
}
func (t *BinTemplate) MustLoadDirectory(directory string) *template.Template {
if tmpl, err := t.LoadDirectory(directory); err != nil {
panic(err)
} else {
return tmpl
}
}