forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_engine.go
31 lines (25 loc) · 934 Bytes
/
template_engine.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
package render
import (
"bytes"
"html/template"
)
// TemplateEngine needs to be implemented for a template system to be able to be used with Buffalo.
type TemplateEngine func(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error)
// GoTemplateEngine implements the TemplateEngine interface for using standard Go templates
func GoTemplateEngine(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error) {
// since go templates don't have the concept of an optional map argument like Plush does
// add this "null" map so it can be used in templates like this:
// {{ partial "flash.html" .nilOpts }}
data["nilOpts"] = map[string]interface{}{}
t := template.New(input)
if helpers != nil {
t = t.Funcs(helpers)
}
t, err := t.Parse(input)
if err != nil {
return "", err
}
bb := &bytes.Buffer{}
err = t.Execute(bb, data)
return bb.String(), err
}