/
mustache.go
66 lines (57 loc) · 1.64 KB
/
mustache.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
package mustache
import (
"github.com/QLeelulu/goku"
"github.com/hoisie/mustache"
"io"
)
// MustacheTemplateEngine
type MustacheTemplateEngine struct {
ExtName string
UseCache bool
TemplateCache map[string]*mustache.Template
}
// template file ext name, default is ".html"
func (te *MustacheTemplateEngine) Ext() string {
if te.ExtName == "" {
return ".html"
}
return te.ExtName
}
// return whether the tempalte support layout
func (te *MustacheTemplateEngine) SupportLayout() bool {
return true
}
func (te *MustacheTemplateEngine) Render(filepath string, layoutPath string, viewData *goku.ViewData, wr io.Writer) {
var tmpl *mustache.Template
tmpl = te.getTemplate(filepath)
var r string
if te.SupportLayout() && layoutPath != "" {
layout := te.getTemplate(layoutPath)
r = tmpl.RenderInLayout(layout, viewData)
} else {
r = tmpl.Render(viewData)
}
wr.Write([]byte(r))
}
func (te *MustacheTemplateEngine) getTemplate(filepath string) *mustache.Template {
var tmpl *mustache.Template
if te.UseCache {
tmpl = te.TemplateCache[filepath]
}
if tmpl == nil {
var err error
tmpl, err = mustache.ParseFile(filepath)
if err != nil {
panic("MustacheTemplateEngine.Render: parse template \"" + filepath + "\" error, " + err.Error())
}
if te.UseCache {
te.TemplateCache[filepath] = tmpl
}
}
return tmpl
}
func NewMustacheTemplateEngine() *MustacheTemplateEngine {
return &MustacheTemplateEngine{
TemplateCache: make(map[string]*mustache.Template),
}
}