-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.go
568 lines (491 loc) · 15 KB
/
views.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package web
import (
"bytes"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
)
// walk recursively in "fs" descends "root" path, calling "walkFn".
func walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error {
names, err := assetNames(fs, root)
if err != nil {
return fmt.Errorf("%s: %w", root, err)
}
for _, name := range names {
fullpath := path.Join(root, name)
f, err := fs.Open(fullpath)
if err != nil {
return fmt.Errorf("%s: %w", fullpath, err)
}
stat, err := f.Stat()
err = walkFn(fullpath, stat, err)
if err != nil {
if err != filepath.SkipDir {
return fmt.Errorf("%s: %w", fullpath, err)
}
continue
}
if stat.IsDir() {
if err := walk(fs, fullpath, walkFn); err != nil {
return fmt.Errorf("%s: %w", fullpath, err)
}
}
}
return nil
}
// assetNames returns the first-level directories and file, sorted, names.
func assetNames(fs http.FileSystem, name string) ([]string, error) {
f, err := fs.Open(name)
if err != nil {
return nil, err
}
if f == nil {
return nil, nil
}
infos, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
names := make([]string, 0, len(infos))
for _, info := range infos {
// note: go-bindata fs returns full names whether
// the http.Dir returns the base part, so
// we only work with their base names.
name := filepath.ToSlash(info.Name())
name = path.Base(name)
names = append(names, name)
}
sort.Strings(names)
return names, nil
}
func asset(fs http.FileSystem, name string) ([]byte, error) {
f, err := fs.Open(name)
if err != nil {
return nil, err
}
contents, err := ioutil.ReadAll(f)
f.Close()
return contents, err
}
func getFS(fsOrDir interface{}) (fs http.FileSystem) {
if fsOrDir == nil {
return noOpFS{}
}
switch v := fsOrDir.(type) {
case string:
if v == "" {
fs = noOpFS{}
} else {
fs = httpDirWrapper{http.Dir(v)}
}
case http.FileSystem:
fs = v
default:
panic(fmt.Errorf(`unexpected "fsOrDir" argument type of %T (string or http.FileSystem)`, v))
}
return
}
type noOpFS struct{}
func (fs noOpFS) Open(name string) (http.File, error) { return nil, nil }
func isNoOpFS(fs http.FileSystem) bool {
_, ok := fs.(noOpFS)
return ok
}
// fixes: "invalid character in file path"
// on amber engine (it uses the virtual fs directly
// and it uses filepath instead of the path package...).
type httpDirWrapper struct {
http.Dir
}
func (fs httpDirWrapper) Open(name string) (http.File, error) {
return fs.Dir.Open(filepath.ToSlash(name))
}
// HTMLEngine contains the html view engine structure.
type HTMLEngine struct {
// the file system to load from.
fs http.FileSystem
// files configuration
rootDir string
extension string
// if true, each time the ExecuteWriter is called the templates will be reloaded,
// each ExecuteWriter waits to be finished before writing to a new one.
reload bool
// parser configuration
options []string // (text) template options
pageDir string
left string
right string
layout string
rmu sync.RWMutex // locks for layoutFuncs and funcs
layoutFuncs template.FuncMap
funcs template.FuncMap
//
middleware func(name string, contents []byte) (string, error)
Templates *template.Template
customCache []customTmp // required to load them again if reload is true.
//
}
type customTmp struct {
name string
contents []byte
funcs template.FuncMap
}
// var (
// _ Engine = (*HTMLEngine)(nil)
// _ EngineFuncer = (*HTMLEngine)(nil)
// )
var emptyFuncs = template.FuncMap{
"yield": func(binding interface{}) (string, error) {
return "", fmt.Errorf("yield was called, yet no layout defined")
},
"section": func() (string, error) {
return "", fmt.Errorf("block was called, yet no layout defined")
},
"partial": func() (string, error) {
return "", fmt.Errorf("block was called, yet no layout defined")
},
"partial_r": func() (string, error) {
return "", fmt.Errorf("block was called, yet no layout defined")
},
"current": func() (string, error) {
return "", nil
},
"html": func() (string, error) {
return "", nil
},
"render": func() (string, error) {
return "", nil
},
}
// HTML creates and returns a new html view engine.
// The html engine used like the "html/template" standard go package
// but with a lot of extra features.
// The given "extension" MUST begin with a dot.
//
// Usage:
// HTML("./views", ".html") or
// HTML(iris.Dir("./views"), ".html") or
// HTML(AssetFile(), ".html") for embedded data.
func HTML(fs interface{}, extension string) *HTMLEngine {
s := &HTMLEngine{
fs: getFS(fs),
rootDir: "/",
extension: extension,
reload: false,
left: "{{",
right: "}}",
pageDir: "",
layout: "",
layoutFuncs: make(template.FuncMap),
funcs: make(template.FuncMap),
}
return s
}
// RootDir sets the directory to be used as a starting point
// to load templates from the provided file system.
func (s *HTMLEngine) RootDir(root string) *HTMLEngine {
s.rootDir = filepath.ToSlash(root)
return s
}
// Ext returns the file extension which this view engine is responsible to render.
func (s *HTMLEngine) Ext() string {
return s.extension
}
// Reload if set 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,
// no concurrent access across clients, use it only on development status.
// It's good to be used side by side with the https://github.com/kataras/rizla reloader for go source files.
func (s *HTMLEngine) Reload(developmentMode bool) *HTMLEngine {
s.reload = developmentMode
return s
}
// PageDir PageDir
func (s *HTMLEngine) PageDir(path string) *HTMLEngine {
s.pageDir = path
return s
}
// Option sets options for the template. Options are described by
// strings, either a simple string or "key=value". There can be at
// most one equals sign in an option string. If the option string
// is unrecognized or otherwise invalid, Option panics.
//
// Known options:
//
// missingkey: Control the behavior during execution if a map is
// indexed with a key that is not present in the map.
// "missingkey=default" or "missingkey=invalid"
// The default behavior: Do nothing and continue execution.
// If printed, the result of the index operation is the string
// "<no value>".
// "missingkey=zero"
// The operation returns the zero value for the map type's element.
// "missingkey=error"
// Execution stops immediately with an error.
//
func (s *HTMLEngine) Option(opt ...string) *HTMLEngine {
s.rmu.Lock()
s.options = append(s.options, opt...)
s.rmu.Unlock()
return s
}
// Delims sets the action delimiters to the specified strings, to be used in
// templates. An empty delimiter stands for the
// corresponding default: {{ or }}.
func (s *HTMLEngine) Delims(left, right string) *HTMLEngine {
s.left, s.right = left, right
return s
}
// Layout sets the layout template file which inside should use
// the {{ yield }} func to yield the main template file
// and optionally {{partial/partial_r/render}} to render other template files like headers and footers
//
// The 'tmplLayoutFile' is a relative path of the templates base directory,
// for the template file with its extension.
//
// Example: HTML("./templates", ".html").Layout("layouts/mainLayout.html")
// // mainLayout.html is inside: "./templates/layouts/".
//
// Note: Layout can be changed for a specific call
// action with the option: "layout" on the iris' context.Render function.
func (s *HTMLEngine) Layout(layoutFile string) *HTMLEngine {
s.layout = layoutFile
return s
}
// AddLayoutFunc adds the function to the template's layout-only function map.
// It is legal to overwrite elements of the default layout actions:
// - yield func() (template.HTML, error)
// - current func() (string, error)
// - partial func(partialName string) (template.HTML, error)
// - partial_r func(partialName string) (template.HTML, error)
// - render func(fullPartialName string) (template.HTML, error).
func (s *HTMLEngine) AddLayoutFunc(funcName string, funcBody interface{}) *HTMLEngine {
s.rmu.Lock()
s.layoutFuncs[funcName] = funcBody
s.rmu.Unlock()
return s
}
// AddFunc adds the function to the template's function map.
// 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).
// - tr func(lang, key string, args ...interface{}) string
func (s *HTMLEngine) AddFunc(funcName string, funcBody interface{}) {
s.rmu.Lock()
s.funcs[funcName] = funcBody
s.rmu.Unlock()
}
// SetFuncs overrides the template funcs with the given "funcMap".
func (s *HTMLEngine) SetFuncs(funcMap template.FuncMap) *HTMLEngine {
s.rmu.Lock()
s.funcs = funcMap
s.rmu.Unlock()
return s
}
// Funcs adds the elements of the argument map to the template's function map.
// It is legal to overwrite elements of the map. The return
// value is the template, so calls can be chained.
func (s *HTMLEngine) Funcs(funcMap template.FuncMap) *HTMLEngine {
s.rmu.Lock()
for k, v := range funcMap {
s.funcs[k] = v
}
s.rmu.Unlock()
return s
}
// Load parses the templates to the engine.
// It's also responsible to add the necessary global functions.
//
// Returns an error if something bad happens, caller is responsible to handle that.
func (s *HTMLEngine) Load() error {
s.rmu.Lock()
defer s.rmu.Unlock()
return s.load()
}
func (s *HTMLEngine) LoadTpls(tpls map[string]string) error {
return nil
}
func (s *HTMLEngine) load() error {
if err := s.reloadCustomTemplates(); err != nil {
return err
}
return walk(s.fs, s.rootDir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if s.extension != "" {
if !strings.HasSuffix(path, s.extension) {
return nil
}
}
buf, err := asset(s.fs, path)
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
return s.parseTemplate(path, buf, nil)
})
}
func (s *HTMLEngine) reloadCustomTemplates() error {
for _, tmpl := range s.customCache {
if err := s.parseTemplate(tmpl.name, tmpl.contents, tmpl.funcs); err != nil {
return err
}
}
return nil
}
// ParseTemplate adds a custom template to the root template.
func (s *HTMLEngine) ParseTemplate(name string, contents []byte, funcs template.FuncMap) (err error) {
s.rmu.Lock()
defer s.rmu.Unlock()
s.customCache = append(s.customCache, customTmp{
name: name,
contents: contents,
funcs: funcs,
})
return s.parseTemplate(name, contents, funcs)
}
func (s *HTMLEngine) parseTemplate(name string, contents []byte, funcs template.FuncMap) (err error) {
s.initRootTmpl()
name = strings.TrimPrefix(name, "/")
tmpl := s.Templates.New(name)
tmpl.Option(s.options...)
var text string
if s.middleware != nil {
text, err = s.middleware(name, contents)
if err != nil {
return
}
} else {
text = string(contents)
}
tmpl.Funcs(emptyFuncs).Funcs(s.funcs)
if len(funcs) > 0 {
tmpl.Funcs(funcs) // custom for this template.
}
_, err = tmpl.Parse(text)
return
}
func (s *HTMLEngine) initRootTmpl() { // protected by the caller.
if s.Templates == nil {
// the root template should be the same,
// no matter how many reloads as the
// following unexported fields cannot be modified.
// However, on reload they should be cleared otherwise we get an error.
s.Templates = template.New(s.rootDir)
s.Templates.Delims(s.left, s.right)
}
}
func (s *HTMLEngine) executeTemplateBuf(name string, binding interface{}) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
err := s.Templates.ExecuteTemplate(buf, name, binding)
return buf, err
}
func (s *HTMLEngine) layoutFuncsFor(lt *template.Template, name string, binding interface{}) {
s.runtimeFuncsFor(lt, name, binding)
funcs := template.FuncMap{
"yield": func() (template.HTML, error) {
buf, err := s.executeTemplateBuf(name, binding)
// Return safe HTML here since we are rendering our own template.
return template.HTML(buf.String()), err
},
}
for k, v := range s.layoutFuncs {
funcs[k] = v
}
lt.Funcs(funcs)
}
func (s *HTMLEngine) runtimeFuncsFor(t *template.Template, name string, binding interface{}) {
funcs := template.FuncMap{
"section": func(partName string, bind ...interface{}) (template.HTML, error) {
// nameTemp := strings.Replace(name, s.extension, "", -1)
fullPartName := fmt.Sprintf("sections/%s%s", partName, s.extension)
if len(bind) > 0 {
binding = bind[0]
}
buf, err := s.executeTemplateBuf(fullPartName, binding)
if err != nil {
return "", nil
}
return template.HTML(buf.String()), err
},
"current": func() (string, error) {
return name, nil
},
"html": func(src string) (template.HTML, error) {
return template.HTML(src), nil
},
"partial": func(partialName string) (template.HTML, error) {
fullPartialName := fmt.Sprintf("%s-%s", partialName, name)
if s.Templates.Lookup(fullPartialName) != nil {
buf, err := s.executeTemplateBuf(fullPartialName, binding)
return template.HTML(buf.String()), err
}
return "", nil
},
// partial related to current page,
// it would be easier for adding pages' style/script inline
// for example when using partial_r '.script' in layout.html
// templates/users/index.html would load templates/users/index.script.html
"partial_r": func(partialName string) (template.HTML, error) {
ext := filepath.Ext(name)
root := name[:len(name)-len(ext)]
fullPartialName := fmt.Sprintf("%s%s%s", root, partialName, ext)
if s.Templates.Lookup(fullPartialName) != nil {
buf, err := s.executeTemplateBuf(fullPartialName, binding)
return template.HTML(buf.String()), err
}
return "", nil
},
"render": func(fullPartialName string) (template.HTML, error) {
buf, err := s.executeTemplateBuf(fullPartialName, binding)
return template.HTML(buf.String()), err
},
}
t.Funcs(funcs)
}
// ExecuteWriter executes a template and writes its result to the w writer.
func (s *HTMLEngine) ExecuteWriter(w io.Writer, name, layout string, bindingData interface{}) error {
// re-parse the templates if reload is enabled.
if s.reload {
s.rmu.Lock()
defer s.rmu.Unlock()
s.Templates = nil
// we lose the templates parsed manually, so store them when it's called
// in order for load to take care of them too.
if err := s.load(); err != nil {
return err
}
}
if len(s.pageDir) > 0 {
name = fmt.Sprintf("%s/%s%s", s.pageDir, name, s.extension)
}
t := s.Templates.Lookup(name)
if t == nil {
return fmt.Errorf("the %s not exist", name)
}
s.runtimeFuncsFor(t, name, bindingData)
if layout == "nolayout" {
return t.Execute(w, bindingData)
}
if layout = getLayout(layout, s.layout); layout != "" {
lt := s.Templates.Lookup(layout + s.extension)
if lt == nil {
return fmt.Errorf("%s not exist", name)
}
s.layoutFuncsFor(lt, name, bindingData)
return lt.Execute(w, bindingData)
}
return t.Execute(w, bindingData)
}