-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
55 lines (51 loc) · 1.19 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
package main
import (
"fmt"
"html/template"
"io/fs"
"path/filepath"
)
// parseTemplateDir parses all the templates in the given directory. Non *.html files are ignored.
func parseTemplateDir(dir string, templateFS fs.FS, funcMap template.FuncMap) (*template.Template, error) {
var paths []string
err := fs.WalkDir(templateFS, dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("error walking templates dir: %v", err)
}
if !d.IsDir() && filepath.Ext(path) == ".html" {
paths = append(paths, path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking templates dir: %v", err)
}
return template.New("").Funcs(funcMap).ParseFS(templateFS, paths...)
}
var commonFuncMap = template.FuncMap{
"mod": func(i, j int) int {
return i % j
},
"sub": func(a, b int) int {
return a - b
},
"add": func(a, b int) int {
return a + b
},
"seq": func(start, end int) []int {
var sequence []int
for i := start; i <= end; i++ {
sequence = append(sequence, i)
}
return sequence
},
"gt": func(a, b int) bool {
return a > b
},
"lt": func(a, b int) bool {
return a < b
},
"eq": func(a, b int) bool {
return a == b
},
}