summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-09-26 20:29:08 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-09-26 20:29:08 +0200
commita89da62db88611c141f123887d0197ca19c32407 (patch)
tree1180c1c3dc0517746cee90b8cec5576a6199e9e1
parent571726b98d2a4a7cce73e6ecddbb36e0771bcfe5 (diff)
Figure out the templates automatically
-rw-r--r--src/templates.go45
-rw-r--r--src/templates/-404.html.tmpl (renamed from src/templates/404.html.tmpl)0
-rw-r--r--src/templates/-base.html.tmpl (renamed from src/templates/base.html.tmpl)0
-rw-r--r--src/templates/-error.html.tmpl (renamed from src/templates/error.html.tmpl)0
-rw-r--r--src/templates/-navbar.html.tmpl (renamed from src/templates/navbar.html.tmpl)0
5 files changed, 29 insertions, 16 deletions
diff --git a/src/templates.go b/src/templates.go
index bf65532..5a4d7c5 100644
--- a/src/templates.go
+++ b/src/templates.go
@@ -3,6 +3,8 @@ package src
import (
"embed"
"html/template"
+ "log"
+ "os"
"strings"
"git.thomasvoss.com/euro-cash.eu/src/mintage"
@@ -18,18 +20,9 @@ type templateData struct {
var (
//go:embed templates/*.html.tmpl
templateFS embed.FS
- notFoundTmpl = buildTemplate("404")
- errorTmpl = buildTemplate("error")
- templates = map[string]*template.Template{
- "/": buildTemplate("index"),
- "/about": buildTemplate("about"),
- "/coins": buildTemplate("coins"),
- "/coins/designs": buildTemplate("coins-designs"),
- "/coins/designs/nl": buildTemplate("coins-designs-nl"),
- "/coins/mintages": buildTemplate("coins-mintages"),
- "/jargon": buildTemplate("jargon"),
- "/language": buildTemplate("language"),
- }
+ notFoundTmpl = buildTemplate("-404")
+ errorTmpl = buildTemplate("-error")
+ templates map[string]*template.Template
funcmap = map[string]any{
"denoms": denoms,
"locales": locales,
@@ -40,15 +33,35 @@ var (
}
)
-func buildTemplate(names ...string) *template.Template {
- names = append([]string{"base", "navbar"}, names...)
+func init() {
+ ents, err := os.ReadDir("src/templates")
+ if err != nil {
+ log.Fatalln(err)
+ }
+ 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, "-", "/")
+ }
+ templates[path] = buildTemplate(name)
+ }
+}
+
+func buildTemplate(name string) *template.Template {
+ names := [...]string{"-base", "-navbar", name}
for i, s := range names {
names[i] = "templates/" + s + ".html.tmpl"
}
return template.Must(template.
- New("base.html.tmpl").
+ New("-base.html.tmpl").
Funcs(funcmap).
- ParseFS(templateFS, names...))
+ ParseFS(templateFS, names[:]...))
}
func asHTML(s string) template.HTML {
diff --git a/src/templates/404.html.tmpl b/src/templates/-404.html.tmpl
index c86dc30..c86dc30 100644
--- a/src/templates/404.html.tmpl
+++ b/src/templates/-404.html.tmpl
diff --git a/src/templates/base.html.tmpl b/src/templates/-base.html.tmpl
index 2a33cc3..2a33cc3 100644
--- a/src/templates/base.html.tmpl
+++ b/src/templates/-base.html.tmpl
diff --git a/src/templates/error.html.tmpl b/src/templates/-error.html.tmpl
index 47bff81..47bff81 100644
--- a/src/templates/error.html.tmpl
+++ b/src/templates/-error.html.tmpl
diff --git a/src/templates/navbar.html.tmpl b/src/templates/-navbar.html.tmpl
index 90f3cc7..90f3cc7 100644
--- a/src/templates/navbar.html.tmpl
+++ b/src/templates/-navbar.html.tmpl