-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpl.go
62 lines (56 loc) · 1.25 KB
/
tmpl.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
// Package tmpl implements a minimal view interface over html/template.
package tmpl
import (
"html/template"
"io"
"io/fs"
"strings"
"sync"
)
// Template represents a set of HTML templates.
type Template struct {
mu sync.Mutex
fs fs.FS
set map[string]*template.Template
}
// Viewable represents a view.
type Viewable interface {
// Templates returns a slice of template names to parse.
Templates() []string
}
// New returns a new template set.
func New(fs fs.FS) *Template {
return &Template{
fs: fs,
set: make(map[string]*template.Template),
}
}
// Render writes the result of applying the templates
// associated with view to the view itself.
func (t *Template) Render(w io.Writer, view Viewable) error {
tmpl, err := t.parse(view)
if err != nil {
return err
}
return tmpl.Execute(w, view)
}
// parse returns the parsed templates representing the view.
func (t *Template) parse(view Viewable) (*template.Template, error) {
if view == nil {
return template.New("nil"), nil
}
names := view.Templates()
key := strings.Join(names, ":")
t.mu.Lock()
defer t.mu.Unlock()
tmpl, ok := t.set[key]
if ok {
return tmpl, nil
}
tmpl, err := template.ParseFS(t.fs, names...)
if err != nil {
return nil, err
}
t.set[key] = tmpl
return tmpl, nil
}