-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
55 lines (50 loc) · 1.32 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
package main
import (
"github.com/apex/log"
"html/template"
"io/ioutil"
"os"
)
func readTemplate(tmplpath string) ([]byte, error) {
tmpl, err := ioutil.ReadFile(tmplpath)
if err != nil {
return nil, err
}
return tmpl, nil
}
// BuildFuncMap merges template.FuncMap's
func BuildFuncMap(funcs ...template.FuncMap) template.FuncMap {
resmap := make(template.FuncMap)
for _, fumap := range funcs {
for k, v := range fumap {
resmap[k] = v
}
}
return resmap
}
// PrepareTemplate creates a template from `tmplpath` initialized with `withfuncs`
func PrepareTemplate(tmplpath string, withfuncs template.FuncMap) (*template.Template, error) {
tmplstr, err := readTemplate(tmplpath)
if err != nil {
return nil, err
}
tmpl := template.Must(template.New("template").Funcs(withfuncs).Parse(string(tmplstr)))
return tmpl, nil
}
// RenderTemplate renders the template and writes the output to `outpath`
func RenderTemplate(tmpl *template.Template, outpath string, cxt ICxt) error {
output, err := os.Create(outpath)
defer output.Close()
if err != nil {
log.WithError(err).Error("Failed to render template")
return err
}
log.Info("Rendering template")
err = tmpl.Execute(output, cxt)
if err != nil {
log.WithError(err).Error("Failed to render template")
return err
}
log.Info("Template successfully rendered")
return nil
}