diff options
Diffstat (limited to 'src/templates.go')
-rw-r--r-- | src/templates.go | 90 |
1 files changed, 45 insertions, 45 deletions
diff --git a/src/templates.go b/src/templates.go index 5a4d7c5..4deeb67 100644 --- a/src/templates.go +++ b/src/templates.go @@ -1,67 +1,78 @@ -package src +package app import ( - "embed" + "fmt" "html/template" + "io/fs" "log" - "os" "strings" - "git.thomasvoss.com/euro-cash.eu/src/mintage" + . "git.thomasvoss.com/euro-cash.eu/pkg/try" + "git.thomasvoss.com/euro-cash.eu/pkg/watch" + + "git.thomasvoss.com/euro-cash.eu/src/dbx" ) type templateData struct { Printer Printer Code, Type string - Mintages mintage.Data + Mintages dbx.MintageData Countries []country } var ( - //go:embed templates/*.html.tmpl - templateFS embed.FS - notFoundTmpl = buildTemplate("-404") - errorTmpl = buildTemplate("-error") - templates map[string]*template.Template - funcmap = map[string]any{ - "denoms": denoms, - "locales": locales, - "safe": asHTML, - "strToCtype": strToCtype, - "toUpper": strings.ToUpper, - "tuple": templateMakeTuple, + notFoundTmpl *template.Template + errorTmpl *template.Template + templates map[string]*template.Template + funcmap = map[string]any{ + "denoms": denoms, + "locales": locales, + "safe": asHTML, + "sprintf": fmt.Sprintf, + "toUpper": strings.ToUpper, + "tuple": templateMakeTuple, } ) -func init() { - ents, err := os.ReadDir("src/templates") - if err != nil { - log.Fatalln(err) - } +func BuildTemplates(dir fs.FS, debugp bool) { + ents := Try2(fs.ReadDir(dir, ".")) + notFoundTmpl = buildTemplate(dir, "-404") + errorTmpl = buildTemplate(dir, "-error") templates = make(map[string]*template.Template, len(ents)) + for _, e := range ents { - path := "/" - name, _ := strings.CutSuffix(e.Name(), ".html.tmpl") - switch { - case name[0] == '-': - continue - case name == "index": - default: - path += strings.ReplaceAll(name, "-", "/") + name := e.Name() + buildAndSetTemplate(dir, name) + if debugp { + go watch.FileFS(dir, name, func() { + buildAndSetTemplate(dir, name) + log.Printf("Template ā%sā updated\n", name) + }) } - templates[path] = buildTemplate(name) } } -func buildTemplate(name string) *template.Template { +func buildAndSetTemplate(dir fs.FS, name string) { + path := "/" + name = strings.TrimSuffix(name, ".html.tmpl") + switch { + case name[0] == '-': + return + case name != "index": + path += strings.ReplaceAll(name, "-", "/") + } + templates[path] = buildTemplate(dir, name) +} + +func buildTemplate(dir fs.FS, name string) *template.Template { names := [...]string{"-base", "-navbar", name} for i, s := range names { - names[i] = "templates/" + s + ".html.tmpl" + names[i] = s + ".html.tmpl" } return template.Must(template. New("-base.html.tmpl"). Funcs(funcmap). - ParseFS(templateFS, names[:]...)) + ParseFS(dir, names[:]...)) } func asHTML(s string) template.HTML { @@ -83,17 +94,6 @@ func templateMakeTuple(args ...any) []any { return args } -func strToCtype(s string) int { - switch s { - case "nifc": - return mintage.TypeNIFC - case "proof": - return mintage.TypeProof - default: - return mintage.TypeCirc - } -} - func (td templateData) T(fmt string, args ...any) string { return td.Printer.T(fmt, args...) } |