-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.go
75 lines (64 loc) · 1.5 KB
/
renderer.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
package tplengine
import (
"github.com/labstack/echo"
"html/template"
"io"
)
type Renderer struct {
*template.Template
}
type DebugRenderer struct {
name string
*template.Template
globFilePatern string
funcMap template.FuncMap
}
func NewDebugRenderer(name string) *DebugRenderer {
return &DebugRenderer{
Template: template.New(name).Funcs(plugins),
name: name,
}
}
func NewRenderer(name string) *Renderer {
return &Renderer{
Template: template.New(name).Funcs(plugins),
}
}
func (r *Renderer) Funcs(funcMap template.FuncMap) *Renderer {
for n, v := range plugins {
funcMap[n] = v
}
r.Template.Funcs(funcMap)
return r
}
func (r *Renderer) Render(w io.Writer, name string, data interface{}, _ echo.Context) error {
return r.ExecuteTemplate(w, name, data)
}
func (r *Renderer) ParseGlob(patern string) error {
_, err := r.Template.ParseGlob(patern)
return err
}
func (r *DebugRenderer) Funcs(funcMap template.FuncMap) *DebugRenderer {
r.funcMap = funcMap
for n, v := range plugins {
funcMap[n] = v
}
r.Template.Funcs(funcMap)
return r
}
func (r *DebugRenderer) Render(w io.Writer, name string, data interface{}, _ echo.Context) error {
r.Template = template.New(r.name)
allPlugins := plugins
if r.funcMap != nil {
for n, v := range r.funcMap {
allPlugins[n] = v
}
}
r.Template.Funcs(allPlugins)
r.Template.ParseGlob(r.globFilePatern)
return r.ExecuteTemplate(w, name, data)
}
func (r *DebugRenderer) ParseGlob(patern string) error {
r.globFilePatern = patern
return nil
}