-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtemplate.go
83 lines (67 loc) · 1.99 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
73
74
75
76
77
78
79
80
81
82
83
package xlog
import (
"bytes"
"embed"
"fmt"
"html/template"
"io/fs"
"log/slog"
"path"
"strings"
)
//go:embed templates
var defaultTemplates embed.FS
var templates *template.Template
var templatesFSs []fs.FS
// RegisterTemplate registers a filesystem that contains templates, specifying subDir as
// the subdirectory name that contains the templates. templates are registered
// such that the latest registered directory override older ones. template file
// extensions are signified by '.html' extension and the file path can
// be used as template name without this extension
func RegisterTemplate(t fs.FS, subDir string) {
ts, _ := fs.Sub(t, subDir)
templatesFSs = append(templatesFSs, ts)
}
func compileTemplates() {
const ext = ".html"
// add default templates before everything else
sub, _ := fs.Sub(defaultTemplates, "templates")
templatesFSs = append([]fs.FS{sub}, templatesFSs...)
templates = template.New("")
for _, tfs := range templatesFSs {
fs.WalkDir(tfs, ".", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(p, ext) && d.Type().IsRegular() {
ext := path.Ext(p)
name := strings.TrimSuffix(p, ext)
slog.Info("Template " + name)
c, err := fs.ReadFile(tfs, p)
if err != nil {
return err
}
template.Must(templates.New(name).Funcs(helpers).Parse(string(c)))
}
return nil
})
}
}
// Partial executes a template by it's path name. it passes data to the
// template. returning the output of the template. in case of an error it will
// return the error string as the output
func Partial(path string, data Locals) template.HTML {
v := templates.Lookup(path)
if v == nil {
return template.HTML(fmt.Sprintf("template %s not found", path))
}
if data == nil {
data = Locals{}
}
data["config"] = Config
w := bytes.NewBufferString("")
if err := v.Execute(w, data); err != nil {
return template.HTML("rendering error " + path + " " + err.Error())
}
return template.HTML(w.String())
}