-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
jet.go
480 lines (398 loc) · 14 KB
/
jet.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package view
import (
"fmt"
"io"
"os"
"path"
"reflect"
"strings"
"github.com/kataras/iris/v12/context"
"github.com/CloudyKit/jet"
)
const jetEngineName = "jet"
// JetEngine is the jet template parser's view engine.
type JetEngine struct {
directory string
extension string
// physical system files or app-embedded, see `Binary(..., ...)`. Defaults to file system on initialization.
loader jet.Loader
developmentMode bool
// The Set is the `*jet.Set`, exported to offer any custom capabilities that jet users may want.
// Available after `Load`.
Set *jet.Set
// Note that global vars and functions are set in a single spot on the jet parser.
// If AddFunc or AddVar called before `Load` then these will be set here to be used via `Load` and clear.
vars map[string]interface{}
jetRangerRendererContextKey string
}
var _ Engine = (*JetEngine)(nil)
// jet library does not export or give us any option to modify them via Set
// (unless we parse the files by ourselves but this is not a smart choice).
var jetExtensions = [...]string{
".html.jet",
".jet.html",
".jet",
}
// Jet creates and returns a new jet view engine.
func Jet(directory, extension string) *JetEngine {
// if _, err := os.Stat(directory); os.IsNotExist(err) {
// panic(err)
// }
extOK := false
for _, ext := range jetExtensions {
if ext == extension {
extOK = true
break
}
}
if !extOK {
panic(fmt.Sprintf("%s extension is not a valid jet engine extension[%s]", extension, strings.Join(jetExtensions[0:], ", ")))
}
s := &JetEngine{
directory: directory,
extension: extension,
loader: jet.NewOSFileSystemLoader(directory),
}
return s
}
// DisableViewDataTypeCheck accepts a context key name to use
// to map the jet specific renderer and ranger over context's view data.
//
// If "jetDataContextKey" is not empty then `ExecuteWriter` will not check for
// types to check if an element passed through `Context.ViewData`
// contains a jet.Renderer or jet.Ranger or both.
// Instead will map those with simple key data naming (faster).
// Also it wont check if a value is already a reflect.Value (jet expects this type as values).
//
// Defaults to empty.
func (s *JetEngine) DisableViewDataTypeCheck(jetRangerRendererContextKey string) *JetEngine {
s.jetRangerRendererContextKey = jetRangerRendererContextKey
return s
}
// String returns the name of this view engine, the "jet".
func (s *JetEngine) String() string {
return jetEngineName
}
// Ext should return the final file extension which this view engine is responsible to render.
func (s *JetEngine) Ext() string {
return s.extension
}
// Delims sets the action delimiters to the specified strings, to be used in
// templates. An empty delimiter stands for the
// corresponding default: {{ or }}.
// Should act before `Load` or `iris.Application#RegisterView`.
func (s *JetEngine) Delims(left, right string) *JetEngine {
s.Set.Delims(left, right)
return s
}
// JetArguments is a type alias of `jet.Arguments`,
// can be used on `AddFunc$funcBody`.
type JetArguments = jet.Arguments
// AddFunc should adds a global function to the jet template set.
func (s *JetEngine) AddFunc(funcName string, funcBody interface{}) {
// if something like "urlpath" is registered.
if generalFunc, ok := funcBody.(func(string, ...interface{}) string); ok {
// jet, unlike others does not accept a func(string, ...interface{}) string,
// instead it wants:
// func(JetArguments) reflect.Value.
s.AddVar(funcName, func(args JetArguments) reflect.Value {
n := args.NumOfArguments()
if n == 0 { // no input, don't execute the function, panic instead.
panic(funcName + " expects one or more input arguments")
}
firstInput := args.Get(0).String()
if n == 1 { // if only the first argument is given.
return reflect.ValueOf(generalFunc(firstInput))
}
// if has variadic.
variadicN := n - 1
variadicInputs := make([]interface{}, variadicN) // except the first one.
for i := 0; i < variadicN; i++ {
variadicInputs[i] = args.Get(i + 1).Interface()
}
return reflect.ValueOf(generalFunc(firstInput, variadicInputs...))
})
return
}
if jetFunc, ok := funcBody.(jet.Func); !ok {
alternativeJetFunc, ok := funcBody.(func(JetArguments) reflect.Value)
if !ok {
panic(fmt.Sprintf("JetEngine.AddFunc: funcBody argument is not a type of func(JetArguments) reflect.Value. Got %T instead", funcBody))
}
s.AddVar(funcName, jet.Func(alternativeJetFunc))
} else {
s.AddVar(funcName, jetFunc)
}
}
// AddVar adds a global variable to the jet template set.
func (s *JetEngine) AddVar(key string, value interface{}) {
if s.Set != nil {
s.Set.AddGlobal(key, value)
} else {
if s.vars == nil {
s.vars = make(map[string]interface{})
}
s.vars[key] = value
}
}
// 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.
//
// Note that if `true` is passed then only one `View -> ExecuteWriter` will be render each time,
// not safe concurrent access across clients, use it only on development state.
func (s *JetEngine) Reload(developmentMode bool) *JetEngine {
s.developmentMode = developmentMode
if s.Set != nil {
s.Set.SetDevelopmentMode(developmentMode)
}
return s
}
// SetLoader can be used when the caller wants to use something like
// multi.Loader or httpfs.Loader of the jet subpackages,
// overrides any previous loader may set by `Binary` or the default.
// Should act before `Load` or `iris.Application#RegisterView`.
func (s *JetEngine) SetLoader(loader jet.Loader) *JetEngine {
s.loader = loader
return s
}
// 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.
// Should act before `Load` or `iris.Application#RegisterView`.
func (s *JetEngine) Binary(assetFn func(name string) ([]byte, error), assetNames func() []string) *JetEngine {
// embedded.
vdir := s.directory
if vdir[0] == '.' {
vdir = vdir[1:]
}
// second check for /something, (or ./something if we had dot on 0 it will be removed)
if vdir[0] == '/' || vdir[0] == os.PathSeparator {
vdir = vdir[1:]
}
// check for trailing slashes because new users may be do that by mistake
// although all examples are showing the correct way but you never know
// i.e "./assets/" is not correct, if was inside "./assets".
// remove last "/".
if trailingSlashIdx := len(vdir) - 1; vdir[trailingSlashIdx] == '/' {
vdir = vdir[0:trailingSlashIdx]
}
namesSlice := assetNames()
names := make(map[string]struct{})
for _, name := range namesSlice {
if !strings.HasPrefix(name, vdir) {
continue
}
extOK := false
fileExt := path.Ext(name)
for _, ext := range jetExtensions {
if ext == fileExt {
extOK = true
break
}
}
if !extOK {
continue
}
names[name] = struct{}{}
}
if len(names) == 0 {
panic("JetEngine.Binary: no embedded files found in directory: " + vdir)
}
s.loader = &embeddedLoader{
vdir: vdir,
asset: assetFn,
names: names,
}
return s
}
type (
embeddedLoader struct {
vdir string
asset func(name string) ([]byte, error)
names map[string]struct{}
}
embeddedFile struct {
contents []byte // the contents are NOT consumed.
readen int64
}
)
var (
_ jet.Loader = (*embeddedLoader)(nil)
_ io.ReadCloser = (*embeddedFile)(nil)
)
func (f *embeddedFile) Close() error { return nil }
func (f *embeddedFile) Read(p []byte) (int, error) {
if f.readen >= int64(len(f.contents)) {
return 0, io.EOF
}
n := copy(p, f.contents[f.readen:])
f.readen += int64(n)
return n, nil
}
// Open opens a file from OS file system.
func (l *embeddedLoader) Open(name string) (io.ReadCloser, error) {
// name = path.Join(l.vdir, name)
contents, err := l.asset(name)
if err != nil {
return nil, err
}
return &embeddedFile{
contents: contents,
}, nil
}
// Exists checks if the template name exists by walking the list of template paths
// returns string with the full path of the template and bool true if the template file was found
func (l *embeddedLoader) Exists(name string) (string, bool) {
fileName := path.Join(l.vdir, name)
if _, ok := l.names[fileName]; ok {
return fileName, true
}
return "", false
}
// Load should load the templates from a physical system directory or by an embedded one (assets/go-bindata).
func (s *JetEngine) Load() error {
s.Set = jet.NewHTMLSetLoader(s.loader)
s.Set.SetDevelopmentMode(s.developmentMode)
if s.vars != nil {
for key, value := range s.vars {
s.Set.AddGlobal(key, value)
}
}
// Note that, unlike the rest of template engines implementations,
// we don't call the Set.GetTemplate to parse the templates,
// we let it to the jet template parser itself which does that at serve-time and caches each template by itself.
return nil
}
type (
// JetRuntimeVars is a type alias for `jet.VarMap`.
// Can be used at `AddJetRuntimeVars/JetEngine.AddRuntimeVars`
// to set a runtime variable ${name} to the executing template.
JetRuntimeVars = jet.VarMap
// JetRuntime is a type alias of `jet.Runtime`,
// can be used on RuntimeVariable input function.
JetRuntime = jet.Runtime
)
// JetRuntimeVarsContextKey is the Iris Context key to keep any custom jet runtime variables.
// See `AddJetRuntimeVars` package-level function and `JetEngine.AddRuntimeVars` method.
const JetRuntimeVarsContextKey = "iris.jetvarmap"
// AddJetRuntimeVars sets or inserts runtime jet variables through the Iris Context.
// This gives the ability to add runtime variables from different handlers in the request chain,
// something that the jet template parser does not offer at all.
//
// Usage: view.AddJetRuntimeVars(ctx, view.JetRuntimeVars{...}).
// See `JetEngine.AddRuntimeVars` too.
func AddJetRuntimeVars(ctx context.Context, jetVarMap JetRuntimeVars) {
if v := ctx.Values().Get(JetRuntimeVarsContextKey); v != nil {
if vars, ok := v.(JetRuntimeVars); ok {
for key, value := range jetVarMap {
vars[key] = value
}
return
}
}
ctx.Values().Set(JetRuntimeVarsContextKey, jetVarMap)
}
// AddRuntimeVars sets or inserts runtime jet variables through the Iris Context.
// This gives the ability to add runtime variables from different handlers in the request chain,
// something that the jet template parser does not offer at all.
//
// Usage: view.AddJetRuntimeVars(ctx, view.JetRuntimeVars{...}).
// See `view.AddJetRuntimeVars` if package-level access is more meanful to the code flow.
func (s *JetEngine) AddRuntimeVars(ctx context.Context, vars JetRuntimeVars) {
AddJetRuntimeVars(ctx, vars)
}
type rangerAndRenderer struct {
ranger jet.Ranger
renderer jet.Renderer
}
func (rr rangerAndRenderer) Range() (reflect.Value, reflect.Value, bool) {
return rr.ranger.Range()
}
func (rr rangerAndRenderer) Render(jetRuntime *jet.Runtime) {
rr.renderer.Render(jetRuntime)
}
func rangerRenderer(bindingData interface{}) (interface{}, bool) {
if ranger, ok := bindingData.(jet.Ranger); ok {
// Externally fixes a BUG on the jet template parser:
// eval.go#executeList(list *ListNode):NodeRange.isSet.getRanger(expression = st.evalPrimaryExpressionGroup)
// which does not act the "ranger" as element, instead is converted to a value of struct, which makes a jet.Ranger func(*myStruct) Range...
// not a compatible jet.Ranger.
// getRanger(st.context) should work but author of the jet library is not currently available,
// to allow a recommentation or a PR and I don't really want to vendor it because
// some end-users may use the jet import path to pass things like Global Funcs and etc.
// So to fix it (at least temporarily and only for ref Ranger) we ptr the ptr the "ranger", not the bindingData, and this may
// have its downside because the same bindingData may be compatible with other node actions like range or custom Render
// but we have no other way at the moment. The same problem exists on the `Renderer` too!
// The solution below fixes the above issue but any fields of the struct are not available,
// this is ok because most of the times if not always, the users of jet don't use fields on Ranger and custom Renderer inside the templates.
if renderer, ok := bindingData.(jet.Renderer); ok {
// this can make a Ranger and Renderer both work together, unlike the jet parser itself.
return rangerAndRenderer{ranger, renderer}, true
}
return &ranger, true
}
if renderer, ok := bindingData.(jet.Renderer); ok {
// Here the fields are not available but usually if completes the jet.Renderer no
// fields are used in the template.
return &renderer, true // see above ^.
}
return nil, false
}
// ExecuteWriter should execute a template by its filename with an optional layout and bindingData.
// See `DisableViewDataTypeCheck` too.
func (s *JetEngine) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error {
tmpl, err := s.Set.GetTemplate(filename)
if err != nil {
return err
}
var vars JetRuntimeVars
if ctx, ok := w.(context.Context); ok {
runtimeVars := ctx.Values().Get(JetRuntimeVarsContextKey)
if runtimeVars != nil {
if jetVars, ok := runtimeVars.(JetRuntimeVars); ok {
vars = jetVars
}
}
}
if bindingData == nil {
return tmpl.Execute(w, vars, nil)
}
jetRangerRenderer, ok := rangerRenderer(bindingData)
if ok {
return tmpl.Execute(w, vars, jetRangerRenderer)
}
if m, ok := bindingData.(context.Map); ok {
for k, v := range m {
if s.jetRangerRendererContextKey == "" {
switch value := v.(type) {
case jet.Ranger, jet.Renderer:
jetRangerRenderer, _ = rangerRenderer(value)
case reflect.Value:
if vars == nil {
vars = make(JetRuntimeVars)
}
// if it's already a reflect value.
vars[k] = value
default:
if vars == nil {
vars = make(JetRuntimeVars)
}
vars.Set(k, v)
}
continue
}
if k == s.jetRangerRendererContextKey {
jetRangerRenderer = v
continue
}
if vars == nil {
vars = make(JetRuntimeVars)
}
vars.Set(k, v)
}
return tmpl.Execute(w, vars, jetRangerRenderer)
}
return tmpl.Execute(w, vars, bindingData)
}