refactor(ui): complete ui template migration and fix playback

This commit is contained in:
2026-05-01 17:28:09 +02:00
committed by Mikkel Elvers
parent 33a939ca81
commit 4f3a61e143
23 changed files with 1298 additions and 68 deletions

View File

@@ -1,6 +1,7 @@
package templates
import (
"encoding/json"
"fmt"
"html/template"
"io"
@@ -36,6 +37,10 @@ func GetRenderer() *Renderer {
}
return m
},
"json": func(v any) template.HTMLAttr {
b, _ := json.Marshal(v)
return template.HTMLAttr(b)
},
}
pages, err := filepath.Glob(filepath.Join(".", "templates", "*.gohtml"))
@@ -55,11 +60,16 @@ func GetRenderer() *Renderer {
}
tmpl := template.New(name).Funcs(funcs)
// Parse base first so it establishes the core definitions
tmpl = template.Must(tmpl.ParseFiles(filepath.Join(".", "templates", "base.gohtml")))
tmpl = template.Must(tmpl.ParseFiles(page))
// Parse all components next so they are available to the page
if len(components) > 0 {
tmpl = template.Must(tmpl.ParseFiles(components...))
}
// Parse the page itself last
tmpl = template.Must(tmpl.ParseFiles(page))
renderer.templates[name] = tmpl
log.Printf("Loaded page template: %s", name)
@@ -75,3 +85,11 @@ func (r *Renderer) ExecuteTemplate(wr io.Writer, name string, data any) error {
}
return tmpl.ExecuteTemplate(wr, "base.gohtml", data)
}
func (r *Renderer) ExecuteFragment(wr io.Writer, name string, block string, data any) error {
tmpl, ok := r.templates[name]
if !ok {
return fmt.Errorf("template %s not found", name)
}
return tmpl.ExecuteTemplate(wr, block, data)
}