-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
54 lines (46 loc) · 1.32 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
package pages
import (
"errors"
"html/template"
)
// Template is a collection of (nested) template files.
type Template struct {
funcMap template.FuncMap
paths []string
template *template.Template
}
// NewTemplate creates a template from template files specified by paths. If the
// template files are supposed to use functions other than the built-in Go
// functions, these functions must be provided through funcMap.
func NewTemplate(funcMap template.FuncMap, paths ...string) (*Template, error) {
if len(paths) == 0 {
return nil, errors.New("pages: no template path provided")
}
tpl, err := load(funcMap, paths...)
if err != nil {
return nil, err
}
return &Template{
funcMap: funcMap,
paths: paths,
template: tpl,
}, nil
}
// MustNewTemplate calls NewTemplate. It panics on error.
func MustNewTemplate(funcMap template.FuncMap, paths ...string) *Template {
template, err := NewTemplate(funcMap, paths...)
if err != nil {
panic(err)
}
return template
}
// Reload parses the template files again.
func (t *Template) Reload() error {
var err error
t.template, err = load(t.funcMap, t.paths...)
return err
}
// load parses all files specified by paths.
func load(funcMap template.FuncMap, paths ...string) (*template.Template, error) {
return template.New("root").Funcs(funcMap).ParseFiles(paths...)
}