-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
433 lines (379 loc) · 12.3 KB
/
templates.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package templates
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"embed"
"log/slog"
"github.com/google/safehtml"
"github.com/google/safehtml/template"
"github.com/google/safehtml/uncheckedconversions"
)
// LayoutContextKey is the key for getting the layout string out of the context
type LayoutContextKey struct{}
const templatesPath = "files/templates"
const layoutsPath = "layouts"
const pagesPath = "pages"
const blocksPath = "blocks"
const fileExtension = ".gohtml"
type Templates struct {
AlwaysReloadAndParseTemplates bool
DefaultLayout string
TemplateFileExtension string
TemplatesPath template.TrustedSource
LayoutsPath string
PagesPath string
BlocksPath string
AddHeadlessCMSFuncMapHelpers bool
funcMap template.FuncMap
fileSystem fs.FS
fileSystemTrusted template.TrustedFS
fileSystemIsEmbed bool
templates map[string]*template.Template
templatesLock sync.RWMutex
}
// New return new Templates with default configs and templates functions to support headless cms
func New(efs *embed.FS, fnMap template.FuncMap) *Templates {
var trustedFileSystem template.TrustedFS
var fileSystem fs.FS
if efs != nil {
fsys, err := fs.Sub(*efs, templatesPath)
if err != nil {
panic(fmt.Errorf("unable to create file system: %w", err))
}
fileSystem = fsys
trustedFileSystem = template.TrustedFSFromEmbed(*efs)
} else {
fileSystem = os.DirFS(templatesPath)
trustedFileSystem = template.TrustedFSFromTrustedSource(template.TrustedSourceFromConstant(templatesPath))
}
t := &Templates{
DefaultLayout: "application",
TemplateFileExtension: ".gohtml",
LayoutsPath: layoutsPath,
PagesPath: pagesPath,
BlocksPath: blocksPath,
AddHeadlessCMSFuncMapHelpers: true, // d_block, trust_html
funcMap: fnMap,
fileSystem: fileSystem,
fileSystemTrusted: trustedFileSystem,
fileSystemIsEmbed: efs != nil,
}
t.AddFuncMapHelpers()
return t
}
func (t *Templates) AddFuncMapHelpers() {
if t.funcMap == nil {
t.funcMap = template.FuncMap{}
}
if t.AddHeadlessCMSFuncMapHelpers {
t.AddDynamicBlockToFuncMap()
t.AddTrustedHTMLToFuncMap()
t.AddLocalsToFuncMap()
}
}
// MustParseTemplates goes fatal if there is an error
func (t *Templates) MustParseTemplates() {
fatalOnErr(t.ParseTemplates())
}
// ParseTemplates reads all html files and freshly compiles the templates
func (t *Templates) ParseTemplates() error {
t.templates = make(map[string]*template.Template)
hfs := http.FS(t.fileSystem)
layouts, err := getFilePathsInDir(hfs, t.LayoutsPath, t.fileSystemIsEmbed)
if err != nil {
return fmt.Errorf("reading layout files: %w", err)
}
numberOfLayouts := len(layouts)
pages, err := getFilePathsInDir(hfs, t.PagesPath, t.fileSystemIsEmbed)
if err != nil {
return fmt.Errorf("reading pages: %w", err)
}
blocks, err := getFilePathsInDir(hfs, t.BlocksPath, t.fileSystemIsEmbed)
if err != nil {
return fmt.Errorf("reading shared blocks: %w", err)
}
if numberOfLayouts == 0 {
return errors.New("you need at least one layout")
}
for _, layoutFilePath := range layouts {
for _, pageFilePath := range pages {
files := append(blocks, pageFilePath, layoutFilePath)
layoutFilename := filepath.Base(layoutFilePath)
layoutName := strings.TrimSuffix(layoutFilename, path.Ext(layoutFilename))
pageFilename := filepath.Base(pageFilePath)
pageName := strings.TrimSuffix(pageFilename, path.Ext(pageFilename))
newTemplate, err := parseNewTemplateWithFuncMap("", t.funcMap, t.fileSystemTrusted, files...)
if err != nil {
return fmt.Errorf("%s: %w", pageName, err)
}
t.templates[layoutName+":"+pageName] = newTemplate // sample 'application:products' aka 'layout:pageName'
}
}
// Page "page" + blocks
for _, pageFilePath := range pages {
files := append(blocks, pageFilePath) // blocks and this one page file will end up in a template
pageFilename := filepath.Base(pageFilePath)
pageName := strings.TrimSuffix(pageFilename, path.Ext(pageFilename))
newTemplate, err := parseNewTemplateWithFuncMap("", t.funcMap, t.fileSystemTrusted, files...)
if err != nil {
return fmt.Errorf("%s: %w", pageName, err)
}
t.templates[":"+pageName] = newTemplate // sample ':products'
}
// Blocks with prefix '_'
for _, blockFilePath := range blocks {
blockFilename := filepath.Base(blockFilePath)
blockName := strings.TrimSuffix(blockFilename, path.Ext(blockFilename))
newTemplate, err := parseNewTemplateWithFuncMap("", t.funcMap, t.fileSystemTrusted, blockFilePath)
if err != nil {
return fmt.Errorf("%s: %w", blockFilePath, err)
}
prefixedBlockName := blockName
if blockName[:1] != "_" {
prefixedBlockName = "_" + blockName
}
if _, exists := t.templates[prefixedBlockName]; exists || !definedTemplatesContain(newTemplate, prefixedBlockName) {
return fmt.Errorf("error reason 1: block already defined as key or reason 2: the filename doesn't match a definition within the file block_filename %s defined_name %s", blockFilename, blockName)
}
t.templates[prefixedBlockName] = newTemplate // sample '_grid'
}
return nil
}
func definedTemplatesContain(t *template.Template, name string) bool {
templates := t.Templates()
for _, tmpl := range templates {
if tmpl.Tree == nil || tmpl.Tree.Root.Pos == 0 {
continue
}
if tmpl.Name() == name {
return true
}
}
return false
}
// ExecuteTemplate renders the template specified by name (layout:page or just page)
func (t *Templates) ExecuteTemplate(w io.Writer, r *http.Request, templateName string, data interface{}) error {
// dev mode for example
if t.AlwaysReloadAndParseTemplates {
if t.templatesLock.TryLock() {
err := t.ParseTemplates()
t.templatesLock.Unlock()
if err != nil {
return err
}
}
t.templatesLock.RLock()
defer t.templatesLock.RUnlock()
}
if templateName == "" {
templateName = "error"
}
// block/snippet/partial
if templateName[:1] == "_" {
tmpl, ok := t.templates[templateName]
if !ok {
return errors.New("template: name not found ->" + templateName)
}
return tmpl.ExecuteTemplate(w, templateName, data) // block has template name defined, so only render that
}
// page only
if templateName[:1] == ":" {
tmpl, ok := t.templates[templateName]
if !ok {
return errors.New("template: name not found ->" + templateName)
}
return tmpl.ExecuteTemplate(w, "page", data) // render page only including its blocks (every page is defined as "page" within the file for layout combination reasons as we don't have yield)
}
// with layout defined in templateName
if strings.Contains(templateName, ":") {
tmpl, ok := t.templates[templateName]
if !ok {
return errors.New("template: name not found ->" + templateName)
}
return tmpl.ExecuteTemplate(w, "layout", data)
}
// with layout [from request-context or default from config]
layoutIsSetInContext := false
if r != nil {
var layout string
layout, layoutIsSetInContext = r.Context().Value(LayoutContextKey{}).(string)
if layoutIsSetInContext {
templateName = fmt.Sprint(layout, ":", templateName)
}
}
if !layoutIsSetInContext {
templateName = fmt.Sprint(t.DefaultLayout, ":", templateName)
}
tmpl, ok := t.templates[templateName]
if !ok {
return errors.New("template: name not found ->" + templateName)
}
return tmpl.ExecuteTemplate(w, "layout", data)
}
// RenderBlockAsHTMLString renders a template from the templates-map as a HTML-String
func (t *Templates) RenderBlockAsHTMLString(blockname string, payload interface{}) (safehtml.HTML, error) {
if blockname[:1] != "_" {
return safehtml.HTML{}, errors.New("blockname needs to start with _")
}
if len(blockname) > 255 {
return safehtml.HTML{}, errors.New("number of characters in string must not exceed 255")
}
b := bytes.Buffer{}
tt, ok := t.templates[blockname]
if !ok {
return safehtml.HTML{}, errors.New("template " + blockname + " not found in templates-map")
}
err := tt.ExecuteTemplate(&b, blockname, payload)
return uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract(b.String()), err
}
// AddDynamicBlockToFuncMap adds 'd_block' to the FuncMap which allows to render blocks from variables dynamically set
// Rob doesn't like, we add it anyway because we like headless cms
// https://stackoverflow.com/questions/28830543/how-to-use-a-field-of-struct-or-variable-value-as-template-name
func (t *Templates) AddDynamicBlockToFuncMap() {
_, ok := t.funcMap["d_block"]
if ok {
slog.Error("func_map", errors.New("d_block is already in use"))
os.Exit(1)
}
t.funcMap["d_block"] = t.RenderBlockAsHTMLString
}
func (t *Templates) AddTrustedHTMLToFuncMap() {
_, ok := t.funcMap["trusted_html"]
if ok {
slog.Error("func_map", errors.New("trusted_html is already in use"))
os.Exit(1)
}
t.funcMap["trusted_html"] = trustedHTML
}
func (t *Templates) AddLocalsToFuncMap() {
_, ok := t.funcMap["locals"]
if ok {
slog.Error("locals", errors.New("locals is already in use"))
os.Exit(1)
}
t.funcMap["locals"] = Locals
}
// HandlerRenderWithData returns a Handler function which only renders the template
func (t *Templates) HandlerRenderWithData(templateName string, data interface{}) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := t.ExecuteTemplate(w, r, templateName, data)
if err != nil {
slog.Error("template", err)
}
}
}
// HandlerRenderWithDataFromContext returns a Handler function which only renders the template and uses data from context
func (t *Templates) HandlerRenderWithDataFromContext(templateName string, contextKey interface{}) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := t.ExecuteTemplate(w, r, templateName, r.Context().Value(contextKey))
if err != nil {
slog.Error("template", err)
}
}
}
// For Testing
func (t *Templates) GetParsedTemplates() []string {
keys := make([]string, 0, len(t.templates))
for k := range t.templates {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (t *Templates) ExecuteTemplateAsText(r *http.Request, templateName string, data interface{}) (string, error) {
b := &bytes.Buffer{}
err := t.ExecuteTemplate(b, r, templateName, data)
if err != nil {
return "", err
}
return b.String(), nil
}
func trustedHTML(html any) safehtml.HTML {
if html == nil {
return uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract("")
}
return uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract(fmt.Sprint(html))
}
func Locals(args ...any) map[string]any {
m := map[string]any{}
var key any
for i, arg := range args {
if i%2 == 0 {
key = arg
} else {
m[fmt.Sprint(key)] = arg
}
}
return m
}
func fatalOnErr(err error) {
if err != nil {
slog.Error("fatal", err)
os.Exit(1)
}
}
func getFilePathsInDir(fs http.FileSystem, dirPath string, prefixTemplatesPath bool) ([]string, error) {
dirPath = cleanPath(dirPath)
dir, err := fs.Open(dirPath)
if err != nil {
return nil, fmt.Errorf("getFilePathsInDir fs.Open: %w", err)
}
fileInfos, err := dir.Readdir(-1)
if err != nil {
return nil, fmt.Errorf("getFilePathsInDir Readdir: %w", err)
}
files := []string{}
for _, fileInfo := range fileInfos {
if path.Ext(fileInfo.Name()) == fileExtension {
if prefixTemplatesPath {
files = append(files, cleanPath(filepath.Join(templatesPath, dirPath, fileInfo.Name())))
} else {
files = append(files, cleanPath(filepath.Join(dirPath, fileInfo.Name())))
}
}
}
return files, nil
}
func parseNewTemplateWithFuncMap(layout string, fnMap template.FuncMap, fs template.TrustedFS, files ...string) (*template.Template, error) {
if len(files) == 0 {
return nil, errors.New("no files in slice")
}
t := template.New(layout).Funcs(fnMap)
t, err := t.ParseFS(fs, files...)
if err != nil {
return nil, err
}
return t, nil
}
// cleanPath returns the canonical path for p, eliminating . and .. elements.
// taken from https://golang.org/src/net/http/server.go?s=68684:68715#L2203
func cleanPath(p string) string {
if p == "" {
return "/"
}
// if p[0] != '/' {
// p = "/" + p
// }
np := path.Clean(p)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if p[len(p)-1] == '/' && np != "/" {
// Fast path for common case of p being the string we want:
if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
np = p
} else {
np += "/"
}
}
return np
}