-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.go
executable file
·56 lines (52 loc) · 1.04 KB
/
templates.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
package webutil
import (
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
)
func LoadTemplates(root string, allowed_suffix []string) (*template.Template, error) {
var tmpl *template.Template
err := filepath.WalkDir(root, func (path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
// Check if the file has any of the required suffixes
allowed := false
for _, s := range allowed_suffix {
if strings.HasSuffix(d.Name(), s) {
allowed = true
break
}
}
if !allowed {
return nil
}
p, err := filepath.Rel(root, path)
if err != nil {
return err
}
p = strings.Replace(p, "\\", "/", -1)
if tmpl == nil {
tmpl = template.New(p)
} else {
tmpl = tmpl.New(p)
}
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
_, err = tmpl.Parse(string(bytes))
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
return tmpl, nil
}