templates: Share fragments

This commit is contained in:
Tom Wiesing 2022-10-20 11:26:35 +02:00
parent dd7be3f520
commit d64e6f8ec3
No known key found for this signature in database
12 changed files with 111 additions and 93 deletions

View file

@ -0,0 +1,30 @@
package static
import (
"embed"
"encoding/json"
"html/template"
)
//go:embed "templates/*.html"
var templates embed.FS
var (
shared *template.Template = template.Must(template.ParseFS(templates, "templates/*.html"))
)
// NewSharedTemplate creates a new template with the given name.
// It will be able to make use of shared templates as well as functions.
func NewSharedTemplate(name string) *template.Template {
new := template.New(name)
new.Funcs(template.FuncMap{
"json": func(data any) (string, error) {
bytes, err := json.Marshal(data)
return string(bytes), err
},
})
for _, template := range shared.Templates() {
new.AddParseTree(template.Tree.Name, template.Tree.Copy())
}
return new
}