-
Notifications
You must be signed in to change notification settings - Fork 251
/
render_pongo2.go
216 lines (187 loc) · 4.88 KB
/
render_pongo2.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package faygo
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
"github.com/andeya/faygo/pongo2"
)
type (
// Tpl is template with modfied time.
Tpl struct {
template *pongo2.Template
modTime time.Time
}
// Render is a custom faygo template renderer using pongo2.
Render struct {
set *pongo2.TemplateSet
tplCache map[string]*Tpl
tplContext pongo2.Context // Context hold globle func for tpl
openCacheFile func(name string) (http.File, error)
caching bool // false=disable caching, true=enable caching
sync.RWMutex
}
)
// New creates a new Render instance with custom Options.
func newRender(openCacheFile func(name string) (http.File, error)) *Render {
return &Render{
set: pongo2.NewSet("faygo", pongo2.DefaultLoader),
tplCache: make(map[string]*Tpl),
tplContext: make(pongo2.Context),
openCacheFile: openCacheFile,
caching: openCacheFile != nil,
}
}
// TemplateVar sets the global template variable or function
func (render *Render) TemplateVar(name string, v interface{}) {
switch d := v.(type) {
case func(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error):
pongo2.RegisterFilter(name, d)
case pongo2.FilterFunction:
pongo2.RegisterFilter(name, d)
default:
render.tplContext[name] = d
}
}
// Render should render the template to the io.Writer.
func (render *Render) Render(filename string, data Map) ([]byte, error) {
if render.caching {
b, _, err := render.fromCache(filename, data, false)
return b, err
}
f, err := os.Open(filename)
if err != nil {
return nil, err
}
var fbytes []byte
fbytes, err = ioutil.ReadAll(f)
if err != nil {
return nil, err
}
fbytes, err = render.RenderFromBytesWithName(filename, fbytes, data)
return fbytes, err
}
// RenderFromBytesWithName should render the template to the io.Writer.
func (render *Render) RenderFromBytesWithName(filename string, fbytes []byte, data Map) ([]byte, error) {
template, err := render.set.FromBytesWithName(filename, fbytes)
if err != nil {
return nil, err
}
var data2 pongo2.Context
if data == nil {
data2 = render.tplContext
} else {
data2 = pongo2.Context(data)
for k, v := range render.tplContext {
if _, ok := data2[k]; !ok {
data2[k] = v
}
}
}
var b bytes.Buffer
err = template.ExecuteWriter(data2, &b)
return b.Bytes(), err
}
func (render *Render) fromCache(fname string, data Map, withInfo bool) ([]byte, os.FileInfo, error) {
// Get file content from the file system cache
f, err := render.openCacheFile(fname)
if err != nil {
render.Lock()
_, has := render.tplCache[fname]
if has {
delete(render.tplCache, fname)
}
render.Unlock()
return nil, nil, errors.New(fname + "is not find.")
}
defer f.Close()
fileInfo, err := f.Stat()
if err != nil {
return nil, nil, err
}
if fileInfo.IsDir() {
return nil, fileInfo, nil
}
render.RLock()
tplObj, has := render.tplCache[fname]
render.RUnlock()
var tpl *pongo2.Template
// When the template cache exists and the file is not updated
if has && tplObj.modTime.Equal(fileInfo.ModTime()) {
tpl = tplObj.template
} else {
// The cache template does not exist or the file is updated
render.Lock()
defer render.Unlock()
// Create a new template and cache it
fbytes, _ := ioutil.ReadAll(f)
tpl, err = render.set.FromBytesWithName(fname, fbytes)
if err != nil {
return nil, nil, err
}
render.tplCache[fname] = &Tpl{template: tpl, modTime: fileInfo.ModTime()}
}
var data2 pongo2.Context
if data == nil {
data2 = render.tplContext
} else {
data2 = pongo2.Context(data)
for k, v := range render.tplContext {
if _, ok := data2[k]; !ok {
data2[k] = v
}
}
}
var b bytes.Buffer
err = tpl.ExecuteWriter(data2, &b)
if withInfo {
return b.Bytes(), newNowFileInfo(fileInfo, int64(b.Len())), err
}
return b.Bytes(), nil, err
}
type nowFileInfo struct {
os.FileInfo
size int64
modTime time.Time
}
// Size returns the size in bytes for regular files; system-dependent for others
func (info *nowFileInfo) Size() int64 {
return info.size
}
// Mode returns file mode bits
func (info *nowFileInfo) ModTime() time.Time {
return info.modTime
}
func newNowFileInfo(info os.FileInfo, size int64) os.FileInfo {
return &nowFileInfo{
FileInfo: info,
size: size,
modTime: time.Now(),
}
}
func (render *Render) renderForFS(filename string, data Map) ([]byte, os.FileInfo, error) {
if render.caching {
return render.fromCache(filename, data, true)
}
f, err := os.Open(filename)
if err != nil {
return nil, nil, err
}
fileInfo, err := f.Stat()
if err != nil {
return nil, nil, err
}
if fileInfo.IsDir() {
return nil, fileInfo, nil
}
var fbytes []byte
fbytes, err = ioutil.ReadAll(f)
if err != nil {
return nil, nil, err
}
fbytes, err = render.RenderFromBytesWithName(filename, fbytes, data)
return fbytes, newNowFileInfo(fileInfo, int64(len(fbytes))), err
}