forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
47 lines (39 loc) · 1.03 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
package template
import (
"io"
"strings"
"text/template"
)
var funcMap = template.FuncMap{
"join": strings.Join,
"sort": sortSeq,
"where": where,
"json": jsonify,
"jsonp": jsonpify,
}
// Template is an interface description for "text/template".
type Template interface {
Execute(wr io.Writer, data interface{}) error
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
}
// NewTemplate returns a new template.
func NewTemplate(
name, format string,
funcs template.FuncMap) (Template, error) {
return newTemplate(name, format, funcs)
}
// MustTemplate returns a new template and panics if the provided template
// format is invalid.
func MustTemplate(name, format string, funcs template.FuncMap) Template {
return template.Must(newTemplate(name, format, funcs))
}
func newTemplate(
name, format string, funcs template.FuncMap) (*template.Template, error) {
if funcs == nil {
funcs = template.FuncMap{}
}
for k, v := range funcMap {
funcs[k] = v
}
return template.New(name).Funcs(funcs).Parse(format)
}