forked from gofiber/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
django.go
247 lines (226 loc) · 5.87 KB
/
django.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
package django
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/flosch/pongo2/v4"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/utils"
)
// Engine struct
type Engine struct {
// 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
layout string
// determines if the engine parsed all templates
loaded bool
// reload on each render
reload bool
// debug prints the parsed templates
debug bool
// lock for funcmap and templates
mutex sync.RWMutex
// template funcmap
funcmap map[string]interface{}
// templates
Templates map[string]*pongo2.Template
}
// New returns a Handlebar render engine for Fiber
func New(directory, extension string) *Engine {
engine := &Engine{
left: "{{",
right: "}}",
directory: directory,
extension: extension,
layout: "embed",
funcmap: make(map[string]interface{}),
}
return engine
}
func NewFileSystem(fs http.FileSystem, extension string) *Engine {
engine := &Engine{
left: "{{",
right: "}}",
directory: "/",
fileSystem: fs,
extension: extension,
layout: "embed",
funcmap: make(map[string]interface{}),
}
return engine
}
// Layout defines the variable name that will incapsulate the template
func (e *Engine) Layout(key string) *Engine {
e.layout = key
return e
}
// Delims sets the action delimiters to the specified strings, to be used in
// templates. An empty delimiter stands for the
// corresponding default: {{ or }}.
func (e *Engine) Delims(left, right string) *Engine {
fmt.Println("delims: this method is not supported for django")
return e
}
// 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
}
// 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.reload = enabled
return e
}
// Debug will print the parsed templates when Load is triggered.
func (e *Engine) Debug(enabled bool) *Engine {
e.debug = enabled
return e
}
// Parse is deprecated, please use Load() instead
func (e *Engine) Parse() error {
fmt.Println("Parse() is deprecated, please use Load() instead.")
return e.Load()
}
// Load parses the templates to the engine.
func (e *Engine) Load() error {
// race safe
e.mutex.Lock()
defer e.mutex.Unlock()
e.Templates = make(map[string]*pongo2.Template)
baseDir := e.directory
var pongoloader pongo2.TemplateLoader
if e.fileSystem != nil {
// ensures creation of httpFileSystemLoader only when filesystem is defined
pongoloader = pongo2.MustNewHttpFileSystemLoader(e.fileSystem, "")
} else {
pongoloader = pongo2.MustNewLocalFileSystemLoader(baseDir)
}
// New pongo2 defaultset
pongoset := pongo2.NewSet("default", pongoloader)
// Set template settings
pongoset.Globals.Update(e.funcmap)
pongo2.SetAutoescape(false)
// Loop trough each directory and register template files
walkFn := func(path string, info os.FileInfo, err error) error {
// 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
}
// Get file extension of file
ext := filepath.Ext(path)
// Skip file if it does not equal the given template extension
if ext != e.extension {
return nil
}
// Get the relative file path
// ./views/html/index.tmpl -> index.tmpl
rel, err := filepath.Rel(e.directory, path)
if err != nil {
return err
}
// Reverse slashes '\' -> '/' and
// partials\footer.tmpl -> partials/footer.tmpl
name := filepath.ToSlash(rel)
// Remove ext from name 'index.tmpl' -> 'index'
name = strings.TrimSuffix(name, e.extension)
// name = strings.Replace(name, e.extension, "", -1)
// Read the file
// #gosec G304
buf, err := utils.ReadFile(path, e.fileSystem)
if err != nil {
return err
}
// Create new template associated with the current one
tmpl, err := pongoset.FromBytes(buf)
if err != nil {
return err
}
e.Templates[name] = tmpl
// Debugging
if e.debug {
fmt.Printf("views: parsed template: %s\n", name)
}
return err
}
// notify engine that we parsed all templates
e.loaded = true
if e.fileSystem != nil {
return utils.Walk(e.fileSystem, e.directory, walkFn)
}
return filepath.Walk(e.directory, walkFn)
}
func getPongoBinding(binding interface{}) pongo2.Context {
if binding == nil {
return nil
}
if binds, ok := binding.(pongo2.Context); ok {
return binds
}
if binds, ok := binding.(map[string]interface{}); ok {
return binds
}
if binds, ok := binding.(fiber.Map); ok {
bind := make(pongo2.Context)
for key, value := range binds {
bind[key] = value
}
return bind
}
return nil
}
// Render will render the template by name
func (e *Engine) Render(out io.Writer, template string, binding interface{}, layout ...string) error {
if !e.loaded || e.reload {
if e.reload {
e.loaded = false
}
if err := e.Load(); err != nil {
return err
}
}
tmpl, ok := e.Templates[template]
if !ok {
return fmt.Errorf("template %s does not exist", template)
}
bind := getPongoBinding(binding)
parsed, err := tmpl.Execute(bind)
if err != nil {
return err
}
if len(layout) > 0 {
if bind == nil {
bind = make(map[string]interface{}, 1)
}
bind[e.layout] = parsed
lay := e.Templates[layout[0]]
if lay == nil {
return fmt.Errorf("layout %s does not exist", layout[0])
}
return lay.ExecuteWriter(bind, out)
}
if _, err = out.Write([]byte(parsed)); err != nil {
return err
}
return nil
}