-
Notifications
You must be signed in to change notification settings - Fork 52
/
jet.go
214 lines (182 loc) · 4.68 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
package jet
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/CloudyKit/jet/v6"
"github.com/CloudyKit/jet/v6/loaders/httpfs"
core "github.com/gofiber/template"
"github.com/gofiber/utils"
)
// Engine struct
type Engine struct {
core.Engine
// templates
Templates *jet.Set
}
// New returns a Jet render engine for Fiber
func New(directory, extension string) *Engine {
// jet library does not export or give us any option to modify the file extension
if extension != ".html.jet" && extension != ".jet.html" && extension != ".jet" {
log.Fatalf("%s Extension is not a valid jet engine ['.html.jet', .jet.html', '.jet']", extension) //nolint:revive // this is not an issue
}
engine := &Engine{
Engine: core.Engine{
Directory: directory,
Extension: extension,
LayoutName: "embed",
Funcmap: make(map[string]interface{}),
},
}
return engine
}
// NewFileSystem returns a Jet render engine for Fiber with file system
func NewFileSystem(fs http.FileSystem, extension string) *Engine {
// jet library does not export or give us any option to modify the file extension
if extension != ".html.jet" && extension != ".jet.html" && extension != ".jet" {
log.Fatalf("%s Extension is not a valid jet engine ['.html.jet', .jet.html', '.jet']", extension) //nolint:revive // this is not an issue
}
engine := &Engine{
Engine: core.Engine{
Directory: "/",
FileSystem: fs,
Extension: extension,
LayoutName: "embed",
Funcmap: make(map[string]interface{}),
},
}
return engine
}
// Load parses the templates to the engine.
func (e *Engine) Load() error {
// race safe
e.Mutex.Lock()
defer e.Mutex.Unlock()
// parse templates
// e.Templates = jet.NewHTMLSet(e.Directory)
var loader jet.Loader
var err error
if e.FileSystem != nil {
loader, err = httpfs.NewLoader(e.FileSystem)
if err != nil {
return err
}
} else {
loader = jet.NewInMemLoader()
}
if e.Verbose {
e.Templates = jet.NewSet(
loader,
jet.WithDelims(e.Left, e.Right),
jet.InDevelopmentMode(),
)
} else {
e.Templates = jet.NewSet(
loader,
jet.WithDelims(e.Left, e.Right),
)
}
for name, fn := range e.Funcmap {
e.Templates.AddGlobal(name, fn)
}
walkFn := func(path string, info os.FileInfo, err error) error {
l := loader.(*jet.InMemLoader) //nolint:errcheck,forcetypeassert // check line 106
// Return error if exist
if err != nil {
return err
}
// Skip file if it's a directory or has no file info
if info == nil || info.IsDir() {
return nil
}
// Skip file if it does not equal the given template Extension
if len(e.Extension) >= len(path) || path[len(path)-len(e.Extension):] != e.Extension {
return nil
}
// ./views/html/index.tmpl -> index.tmpl
rel, err := filepath.Rel(e.Directory, path)
if err != nil {
return err
}
name := strings.TrimSuffix(rel, e.Extension)
// Read the file
// #gosec G304
buf, err := utils.ReadFile(path, e.FileSystem)
if err != nil {
return err
}
l.Set(name, string(buf))
if e.Verbose {
log.Printf("views: parsed template: %s\n", name)
}
return err
}
// notify Engine that we parsed all templates
e.Loaded = true
if _, ok := loader.(*jet.InMemLoader); ok {
return filepath.Walk(e.Directory, walkFn)
}
return err
}
// Render will render the template by name
func (e *Engine) Render(out io.Writer, name string, binding interface{}, layout ...string) error {
// Check if templates need to be loaded/reloaded
if e.PreRenderCheck() {
if err := e.Load(); err != nil {
return err
}
}
// Acquire read lock for accessing the template
e.Mutex.RLock()
tmpl, err := e.Templates.GetTemplate(name)
e.Mutex.RUnlock()
if err != nil || tmpl == nil {
return fmt.Errorf("render: template %s could not be Loaded: %w", name, err)
}
// Lock while executing layout
e.Mutex.Lock()
defer e.Mutex.Unlock()
bind := jetVarMap(binding)
if len(layout) > 0 && layout[0] != "" {
lay, err := e.Templates.GetTemplate(layout[0])
if err != nil {
return err
}
var renderingError error
bind.Set(e.LayoutName, func() {
renderingError = tmpl.Execute(out, bind, nil)
})
err = lay.Execute(out, bind, nil)
if renderingError != nil {
return renderingError
}
return err
}
return tmpl.Execute(out, bind, nil)
}
func jetVarMap(binding interface{}) jet.VarMap {
var bind jet.VarMap
if binding == nil {
return bind
}
switch binds := binding.(type) {
case map[string]interface{}:
bind = make(jet.VarMap)
for key, value := range binds {
bind.Set(key, value)
}
case fiber.Map:
bind = make(jet.VarMap)
for key, value := range binds {
bind.Set(key, value)
}
case jet.VarMap:
bind = binds
}
return bind
}