forked from gofiber/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
104 lines (93 loc) · 2.65 KB
/
template.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
package template
import (
"io"
"net/http"
"sync"
)
// IEngine interface, to be implemented for any templating engine added to the repository
type IEngine interface {
IEngineCore
Load() error
Render(out io.Writer, template string, binding interface{}, layout ...string) error
}
// IEngineCore interface
type IEngineCore interface {
AddFunc(name string, fn interface{}) IEngineCore
AddFuncMap(m map[string]interface{}) IEngineCore
Debug(enabled bool) IEngineCore
Delims(left, right string) IEngineCore
FuncMap() map[string]interface{}
Layout(key string) IEngineCore
Reload(enabled bool) IEngineCore
}
// Engine engine struct
type Engine struct {
IEngineCore
// delimiters
Left string
Right string
// views folder
Directory string
// http.FileSystem supports embedded files
FileSystem http.FileSystem
// views extension
Extension string
// layout variable name that incapsulates the template
LayoutName string
// determines if the engine parsed all templates
Loaded bool
// reload on each render
ShouldReload bool
// debug prints the parsed templates
Verbose bool
// lock for funcmap and templates
Mutex sync.RWMutex
// template funcmap
Funcmap map[string]interface{}
}
// AddFunc adds the function to the template's function map.
// It is legal to overwrite elements of the default actions
func (e *Engine) AddFunc(name string, fn interface{}) *Engine {
e.Mutex.Lock()
e.Funcmap[name] = fn
e.Mutex.Unlock()
return e
}
// AddFuncMap adds the functions from a map to the template's function map.
// It is legal to overwrite elements of the default actions
func (e *Engine) AddFuncMap(m map[string]interface{}) *Engine {
e.Mutex.Lock()
for name, fn := range m {
e.Funcmap[name] = fn
}
e.Mutex.Unlock()
return e
}
// Debug will print the parsed templates when Load is triggered.
func (e *Engine) Debug(enabled bool) *Engine {
e.Verbose = enabled
return e
}
// Delims sets the action delimiters to the specified strings, to be used in
// templates. An empty delimiter stands for the
// corresponding default: "{{" and "}}".
func (e *Engine) Delims(left, right string) *Engine {
e.Left, e.Right = left, right
return e
}
// FuncMap returns the template's function map.
func (e *Engine) FuncMap() map[string]interface{} {
return e.Funcmap
}
// Layout defines the variable name that will incapsulate the template
func (e *Engine) Layout(key string) *Engine {
e.LayoutName = key
return e
}
// Reload if set to true the templates are reloading on each render,
// use it when you're in development and you don't want to restart
// the application when you edit a template file.
func (e *Engine) Reload(enabled bool) *Engine {
e.ShouldReload = enabled
return e
}