forked from qor/qor
/
template.go
58 lines (48 loc) · 1.01 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
package admin
import (
"errors"
"os"
"path"
"regexp"
"strings"
"html/template"
)
var layouts = map[string]*template.Template{}
var templates = map[string]*template.Template{}
var tmplSuffix = regexp.MustCompile(`(\.tmpl)?$`)
var viewPaths = []string{}
var Root, _ = os.Getwd()
func init() {
if root := os.Getenv("WEB_ROOT"); root != "" {
Root = root
}
for _, dir := range []string{path.Join(Root, "app/views/qor")} {
RegisterViewPath(dir)
}
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
RegisterViewPath(path.Join(gopath, "src/github.com/qor/qor/admin/views"))
}
}
func RegisterViewPath(path string) error {
if isExistingDir(path) {
var found bool
for _, viewPath := range viewPaths {
if path == viewPath {
found = true
break
}
}
if !found {
viewPaths = append(viewPaths, path)
}
return nil
}
return errors.New("path not found")
}
func isExistingDir(pth string) bool {
fi, err := os.Stat(pth)
if err != nil {
return false
}
return fi.Mode().IsDir()
}