-
Notifications
You must be signed in to change notification settings - Fork 43
/
template.go
72 lines (59 loc) · 1.49 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
63
64
65
66
67
68
69
70
71
72
package neo
import (
"errors"
"html/template"
"io"
"path/filepath"
)
// todo: move global Template instance to some local scope
var tpl *template.Template
// Write result of ExecuteTemplate to provided writer. This is usually ResponseWriter.
func renderTpl(writer io.Writer, name string, data interface{}) error {
if tpl == nil {
return errors.New("Rendering template error! Please compile your templates.")
}
return tpl.ExecuteTemplate(writer, name, data)
}
// Collect all files for come location.
func collectFiles(location string) ([]string, error) {
log.Debug("collecting templates from " + location)
result := []string{}
paths, err := filepath.Glob(location)
if err != nil {
log.Error(err.Error())
return nil, err
}
for _, path := range paths {
isDir, err := isDirectory(path)
if err != nil {
log.Error(err.Error())
return nil, err
}
if !isDir {
result = append(result, path)
}
}
return result, nil
}
// compile templates on provided locations
// method will panic if there is something wrong during template compilation
// this is intended to be called on application startup
func compileTpl(paths ...string) {
locations := []string{}
var err error
for _, path := range paths {
pths, err := collectFiles(path)
if err != nil {
panic(err)
} else {
locations = append(locations, pths...)
}
}
tpl, err = template.ParseFiles(locations...)
if err != nil {
panic(err)
}
}
func tplExists(name string) bool {
return tpl != nil && tpl.Lookup(name) != nil
}