-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.go
57 lines (45 loc) · 1.21 KB
/
assets.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
package main
import (
"embed"
"html/template"
"io"
"io/fs"
"github.com/hexpunk/inventory/htmltemplate"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
//go:embed migrations
var migrations embed.FS
//go:embed node_modules/bootstrap/dist/js/bootstrap.js
//go:embed node_modules/bootstrap/dist/js/bootstrap.min.js
//go:embed node_modules/bootstrap/dist/css/bootstrap.css
//go:embed node_modules/bootstrap/dist/css/bootstrap.min.css
//go:embed node_modules/htmx.org/dist/htmx.js
//go:embed node_modules/htmx.org/dist/htmx.min.js
var nodeModules embed.FS
//go:embed templates
var templateFS embed.FS
var parsedTemplates *template.Template
func parseTemplates() error {
if parsedTemplates == nil {
dirfs, err := fs.Sub(templateFS, "templates")
if err != nil {
return err
}
parsedTemplates, err = htmltemplate.ParseFS(dirfs, "*.go.html", "*/*.go.html")
if err != nil {
return err
}
log.Logger.Trace().Func(func(e *zerolog.Event) {
e.Strs("templates", htmltemplate.Names(parsedTemplates))
}).Send()
}
return nil
}
func RenderTemplate(w io.Writer, templateName string) error {
err := parseTemplates()
if err != nil {
return err
}
return parsedTemplates.ExecuteTemplate(w, templateName, nil)
}