-
Notifications
You must be signed in to change notification settings - Fork 53
/
template.go
129 lines (116 loc) · 3.16 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
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
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
PreRenderCheck() bool
}
// 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{}) IEngineCore {
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{}) IEngineCore {
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) IEngineCore {
e.Mutex.Lock()
e.Verbose = enabled
e.Mutex.Unlock()
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) IEngineCore {
e.Mutex.Lock()
e.Left, e.Right = left, right
e.Mutex.Unlock()
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) IEngineCore {
e.Mutex.Lock()
e.LayoutName = key
e.Mutex.Unlock()
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) IEngineCore {
e.Mutex.Lock()
e.ShouldReload = enabled
e.Mutex.Unlock()
return e
}
// Check if the engine should reload the templates before rendering
// Explicit Mute Unlock vs defer offers better performance
func (e *Engine) PreRenderCheck() bool {
e.Mutex.Lock()
if !e.Loaded || e.ShouldReload {
if e.ShouldReload {
e.Loaded = false
}
e.Mutex.Unlock()
return true
}
e.Mutex.Unlock()
return false
}