generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplates.go
95 lines (82 loc) · 3.05 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
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
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"html/template"
"io/fs"
"github.com/pkg/errors"
// some templates functions we might use
// "github.com/Masterminds/sprig/v3"
)
var templates = template.Must(ParseFSStrict(resources, "templates"))
// ParseFSStrict works like template.ParseFS, but is more strict:
// - each template will be given the same name as the file it is defined in
// - each filename can contain only one template and may not {{define}} subtemplates
// - filenames must end in .tmpl
//
// This approach eliminates the possibility of inconsistency between the names
// of templates and the names of template files, reducing decision overhead
// and opportunities for surprises for developers. It also eliminates the
// possibility of two templates accidentally being given the same name, which
// will result in one template being overwritten by the other and can create
// surprising bugs (this was the immediate motivation for creating this
// function).
//
// The returned template's name will have the base name and parsed contents of
// the first file. There must be at least one file. If an error occurs,
// parsing stops and the returned *Template is nil.
//
// Templates in subdirectories of the provided directory will be parsed. The
// names of templates in subdirectories will be prefixed with the name
// of subdirectory (e.g. "charts/chart1.html.tmpl")
//
// TODO: submit pull request to add this to the html/template library.
func ParseFSStrict(resources fs.FS, dir string) (*template.Template, error) {
var ts *template.Template
templateFiles, err := fs.ReadDir(resources, dir)
if err != nil {
return ts, errors.Wrapf(err, "fs.ReadDir(%s)", dir)
}
for _, dirEntry := range templateFiles {
if dirEntry.IsDir() {
subDirName := dir + "/" + dirEntry.Name()
subTemplates, err := ParseFSStrict(resources, subDirName)
if err != nil {
return ts, errors.Wrapf(err, "fs.ReadDir(%s)", subDirName)
}
for _, t := range subTemplates.Templates() {
fileName := dirEntry.Name() + "/" + t.Name()
if ts == nil {
ts = t
}
_, err := ts.AddParseTree(fileName, t.Tree)
if err != nil {
return ts, errors.Wrapf(err, "ts.AddParseTree(%s)", fileName)
}
}
continue
}
fileName := dirEntry.Name()
// use this to add sprig functions.
// t, err := template.New(fileName).Funcs(sprig.FuncMap()).ParseFS(resources, dir+"/"+fileName)
t, err := template.New(fileName).ParseFS(resources, dir+"/"+fileName)
if err != nil {
return ts, errors.Wrapf(err, "parsing template %s", dir+"/"+fileName)
}
for _, t := range t.Templates() {
if t.Name() != fileName {
return ts, fmt.Errorf(`{{define "%v"}} in file %v not allowed when using ParseFSStrict. Each template file must contain one template whose name will be equal to the filename.`, t.Name(), fileName)
}
}
if ts == nil {
ts = t
}
_, err = ts.AddParseTree(fileName, t.Tree)
if err != nil {
return ts, errors.Wrapf(err, "ts.AddParseTree(%s)", fileName)
}
}
if ts == nil {
return ts, fmt.Errorf("No template files found in directory %s", dir)
}
return ts, nil
}