-
Notifications
You must be signed in to change notification settings - Fork 54
/
html.go
174 lines (150 loc) · 4.21 KB
/
html.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
package html
import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
core "github.com/gofiber/template"
"github.com/gofiber/utils"
)
// Engine struct
type Engine struct {
core.Engine
// templates
Templates *template.Template
}
// New returns a HTML render engine for Fiber
func New(directory, extension string) *Engine {
return newEngine(directory, extension, nil)
}
// NewFileSystem returns a HTML render engine for Fiber with file system
func NewFileSystem(fs http.FileSystem, extension string) *Engine {
return newEngine("/", extension, fs)
}
// newEngine creates a new Engine instance with common initialization logic.
func newEngine(directory, extension string, fs http.FileSystem) *Engine {
engine := &Engine{
Engine: core.Engine{
Left: "{{",
Right: "}}",
Directory: directory,
FileSystem: fs,
Extension: extension,
LayoutName: "embed",
Funcmap: make(map[string]interface{}),
},
}
// Add a default function that throws an error if called unexpectedly.
// This can be useful for debugging or ensuring certain functions are used correctly.
engine.AddFunc(engine.LayoutName, func() error {
return fmt.Errorf("layoutName called unexpectedly")
})
return engine
}
// Load parses the templates to the engine.
func (e *Engine) Load() error {
if e.Loaded {
return nil
}
// race safe
e.Mutex.Lock()
defer e.Mutex.Unlock()
e.Templates = template.New(e.Directory)
// Set template settings
e.Templates.Delims(e.Left, e.Right)
e.Templates.Funcs(e.Funcmap)
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
}
// 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
}
// 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
// This enable use to invoke other templates {{ template .. }}
_, err = e.Templates.New(name).Parse(string(buf))
if err != nil {
return err
}
// Debugging
if e.Verbose {
log.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)
}
// Render will execute the template name along with the given values.
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 := e.Templates.Lookup(name)
e.Mutex.RUnlock()
if tmpl == nil {
return fmt.Errorf("render: template %s does not exist", name)
}
render := renderFuncCreate(e, out, binding, *tmpl, nil)
if len(layout) > 0 && layout[0] != "" {
e.Mutex.Lock()
defer e.Mutex.Unlock()
}
// construct a nested render function to embed templates in layouts
for _, layName := range layout {
if layName == "" {
break
}
lay := e.Templates.Lookup(layName)
if lay == nil {
return fmt.Errorf("render: LayoutName %s does not exist", layName)
}
render = renderFuncCreate(e, out, binding, *lay, render)
}
return render()
}
func renderFuncCreate(e *Engine, out io.Writer, binding interface{}, tmpl template.Template, childRenderFunc func() error) func() error {
return func() error {
tmpl.Funcs(map[string]interface{}{
e.LayoutName: childRenderFunc,
})
return tmpl.Execute(out, binding)
}
}