forked from kataras/iris
/
django.go
308 lines (258 loc) · 8.7 KB
/
django.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package view
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/flosch/pongo2"
"github.com/kataras/iris/context"
)
type (
// Value conversion for pongo2.Value
Value pongo2.Value
// Error conversion for pongo2.Error
Error pongo2.Error
// FilterFunction conversion for pongo2.FilterFunction
FilterFunction func(in *Value, param *Value) (out *Value, err *Error)
)
// DjangoEngine contains the amber view engine structure.
type DjangoEngine struct {
// files configuration
directory string
extension string
assetFn func(name string) ([]byte, error) // for embedded, in combination with directory & extension
namesFn func() []string // for embedded, in combination with directory & extension
reload bool
//
rmu sync.RWMutex // locks for filters and globals
// filters for pongo2, map[name of the filter] the filter function . The filters are auto register
filters map[string]FilterFunction
// globals share context fields between templates. https://github.com/flosch/pongo2/issues/35
globals map[string]interface{}
mu sync.Mutex // locks for template cache
templateCache map[string]*pongo2.Template
}
var _ Engine = &DjangoEngine{}
// Django creates and returns a new amber view engine.
func Django(directory, extension string) *DjangoEngine {
s := &DjangoEngine{
directory: directory,
extension: extension,
globals: make(map[string]interface{}, 0),
filters: make(map[string]FilterFunction, 0),
templateCache: make(map[string]*pongo2.Template, 0),
}
return s
}
// Ext returns the file extension which this view engine is responsible to render.
func (s *DjangoEngine) Ext() string {
return s.extension
}
// Binary optionally, use it when template files are distributed
// inside the app executable (.go generated files).
//
// The assetFn and namesFn can come from the go-bindata library.
func (s *DjangoEngine) Binary(assetFn func(name string) ([]byte, error), namesFn func() []string) *DjangoEngine {
s.assetFn, s.namesFn = assetFn, namesFn
return s
}
// Reload if setted to true the templates are reloading on each render,
// use it when you're in development and you're boring of restarting
// the whole app when you edit a template file.
func (s *DjangoEngine) Reload(developmentMode bool) *DjangoEngine {
s.reload = developmentMode
return s
}
// AddFunc adds the function to the template's Globals.
// It is legal to overwrite elements of the default actions:
// - url func(routeName string, args ...string) string
// - urlpath func(routeName string, args ...string) string
// - render func(fullPartialName string) (template.HTML, error).
func (s *DjangoEngine) AddFunc(funcName string, funcBody interface{}) {
s.rmu.Lock()
s.globals[funcName] = funcBody
s.rmu.Unlock()
}
// AddFilter adds a filter to the template.
func (s *DjangoEngine) AddFilter(filterName string, filterBody FilterFunction) *DjangoEngine {
s.rmu.Lock()
s.filters[filterName] = filterBody
s.rmu.Unlock()
return s
}
// Load parses the templates to the engine.
// It's alos responsible to add the necessary global functions.
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *DjangoEngine) Load() error {
if s.assetFn != nil && s.namesFn != nil {
// embedded
return s.loadAssets()
}
// load from directory, make the dir absolute here too.
dir, err := filepath.Abs(s.directory)
if err != nil {
return err
}
// change the directory field configuration, load happens after directory has been setted, so we will not have any problems here.
s.directory = dir
return s.loadDirectory()
}
// this exists because of moving the pongo2 to the vendors without conflictitions if users
// wants to register pongo2 filters they can use this django.FilterFunc to do so.
func (s *DjangoEngine) convertFilters() map[string]pongo2.FilterFunction {
filters := make(map[string]pongo2.FilterFunction, len(s.filters))
for k, v := range s.filters {
func(filterName string, filterFunc FilterFunction) {
fn := pongo2.FilterFunction(func(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
theOut, theErr := filterFunc((*Value)(in), (*Value)(param))
return (*pongo2.Value)(theOut), (*pongo2.Error)(theErr)
})
filters[filterName] = fn
}(k, v)
}
return filters
}
// LoadDirectory loads the templates from directory.
func (s *DjangoEngine) loadDirectory() (templateErr error) {
dir, extension := s.directory, s.extension
fsLoader, err := pongo2.NewLocalFileSystemLoader(dir) // I see that this doesn't read the content if already parsed, so do it manually via filepath.Walk
if err != nil {
return err
}
set := pongo2.NewSet("", fsLoader)
set.Globals = getPongoContext(s.globals)
// set the filters
filters := s.convertFilters()
for filterName, filterFunc := range filters {
pongo2.RegisterFilter(filterName, filterFunc)
}
s.mu.Lock()
defer s.mu.Unlock()
// Walk the supplied directory and compile any files that match our extension list.
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// Fix same-extension-dirs bug: some dir might be named to: "users.tmpl", "local.html".
// These dirs should be excluded as they are not valid golang templates, but files under
// them should be treat as normal.
// If is a dir, return immediately (dir is not a valid golang template).
if info == nil || info.IsDir() {
} else {
rel, err := filepath.Rel(dir, path)
if err != nil {
templateErr = err
return err
}
ext := filepath.Ext(rel)
if ext == extension {
buf, err := ioutil.ReadFile(path)
if err != nil {
templateErr = err
return err
}
name := filepath.ToSlash(rel)
s.templateCache[name], templateErr = set.FromString(string(buf))
if templateErr != nil {
return templateErr
}
}
}
return nil
})
return
}
// loadAssets loads the templates by binary (go-bindata for embedded).
func (s *DjangoEngine) loadAssets() error {
virtualDirectory, virtualExtension := s.directory, s.extension
assetFn, namesFn := s.assetFn, s.namesFn
var templateErr error
/*fsLoader, err := pongo2.NewLocalFileSystemLoader(virtualDirectory)
if err != nil {
return err
}*/
set := pongo2.NewSet("", pongo2.DefaultLoader)
set.Globals = getPongoContext(s.globals)
// set the filters
filters := s.convertFilters()
for filterName, filterFunc := range filters {
pongo2.RegisterFilter(filterName, filterFunc)
}
if len(virtualDirectory) > 0 {
if virtualDirectory[0] == '.' { // first check for .wrong
virtualDirectory = virtualDirectory[1:]
}
if virtualDirectory[0] == '/' || virtualDirectory[0] == os.PathSeparator { // second check for /something, (or ./something if we had dot on 0 it will be removed
virtualDirectory = virtualDirectory[1:]
}
}
s.mu.Lock()
defer s.mu.Unlock()
names := namesFn()
for _, path := range names {
if !strings.HasPrefix(path, virtualDirectory) {
continue
}
rel, err := filepath.Rel(virtualDirectory, path)
if err != nil {
templateErr = err
return err
}
ext := filepath.Ext(rel)
if ext == virtualExtension {
buf, err := assetFn(path)
if err != nil {
templateErr = err
return err
}
name := filepath.ToSlash(rel)
s.templateCache[name], err = set.FromString(string(buf))
if err != nil {
templateErr = err
return err
}
}
}
return templateErr
}
// getPongoContext returns the pongo2.Context from map[string]interface{} or from pongo2.Context, used internaly
func getPongoContext(templateData interface{}) pongo2.Context {
if templateData == nil {
return nil
}
if contextData, isPongoContext := templateData.(pongo2.Context); isPongoContext {
return contextData
}
if contextData, isContextViewData := templateData.(context.Map); isContextViewData {
return pongo2.Context(contextData)
}
return templateData.(map[string]interface{})
}
func (s *DjangoEngine) fromCache(relativeName string) *pongo2.Template {
s.mu.Lock()
tmpl, ok := s.templateCache[relativeName]
if ok {
s.mu.Unlock()
return tmpl
}
s.mu.Unlock()
return nil
}
// ExecuteWriter executes a templates and write its results to the w writer
// layout here is useless.
func (s *DjangoEngine) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error {
// reload the templates if reload configuration field is true
if s.reload {
if err := s.Load(); err != nil {
return err
}
}
if tmpl := s.fromCache(filename); tmpl != nil {
return tmpl.ExecuteWriter(getPongoContext(bindingData), w)
}
return fmt.Errorf("template with name %s doesn't exists in the dir", filename)
}