From a89da62db88611c141f123887d0197ca19c32407 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 26 Sep 2024 20:29:08 +0200 Subject: Figure out the templates automatically --- src/templates.go | 45 +++++--- src/templates/-404.html.tmpl | 16 +++ src/templates/-base.html.tmpl | 42 ++++++++ src/templates/-error.html.tmpl | 23 ++++ src/templates/-navbar.html.tmpl | 228 ++++++++++++++++++++++++++++++++++++++++ src/templates/404.html.tmpl | 16 --- src/templates/base.html.tmpl | 42 -------- src/templates/error.html.tmpl | 23 ---- src/templates/navbar.html.tmpl | 228 ---------------------------------------- 9 files changed, 338 insertions(+), 325 deletions(-) create mode 100644 src/templates/-404.html.tmpl create mode 100644 src/templates/-base.html.tmpl create mode 100644 src/templates/-error.html.tmpl create mode 100644 src/templates/-navbar.html.tmpl delete mode 100644 src/templates/404.html.tmpl delete mode 100644 src/templates/base.html.tmpl delete mode 100644 src/templates/error.html.tmpl delete mode 100644 src/templates/navbar.html.tmpl (limited to 'src') 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 new file mode 100644 index 0000000..c86dc30 --- /dev/null +++ b/src/templates/-404.html.tmpl @@ -0,0 +1,16 @@ +{{ define "content" }} +
+ {{ template "navbar" . }} +

{{ .T "Page Not Found" }}

+
+
+

+ {{ .T ` + The page you were looking for does not exist. If you believe this + is a mistake then don’t hesitate to contact @onetruemangoman on + Discord or email us at %s.` + `mail@euro-cash.eu` | safe + }} +

+
+{{ end }} diff --git a/src/templates/-base.html.tmpl b/src/templates/-base.html.tmpl new file mode 100644 index 0000000..2a33cc3 --- /dev/null +++ b/src/templates/-base.html.tmpl @@ -0,0 +1,42 @@ + + + + + + + {{ .T "Euro Cash" }} + + + + {{ template "content" . }} + + + diff --git a/src/templates/-error.html.tmpl b/src/templates/-error.html.tmpl new file mode 100644 index 0000000..47bff81 --- /dev/null +++ b/src/templates/-error.html.tmpl @@ -0,0 +1,23 @@ +{{ define "content" }} +
+ {{ template "navbar" . }} +

{{ .Code }} {{ .Msg }}

+
+
+

+ {{ .T ` + If you’re seeing this page, it means that something went wrong on + our end that we need to fix. Our team has been notified of this + error, and we apologise for the inconvenience. + ` }} +

+

+ {{ .T ` + If this issue persists, don’t hesitate to contact @onetruemangoman + on Discord or to email us at %s.` + `` | safe + }} +

+
+{{ end }} diff --git a/src/templates/-navbar.html.tmpl b/src/templates/-navbar.html.tmpl new file mode 100644 index 0000000..90f3cc7 --- /dev/null +++ b/src/templates/-navbar.html.tmpl @@ -0,0 +1,228 @@ +{{ define "navbar" }} + +{{ end }} diff --git a/src/templates/404.html.tmpl b/src/templates/404.html.tmpl deleted file mode 100644 index c86dc30..0000000 --- a/src/templates/404.html.tmpl +++ /dev/null @@ -1,16 +0,0 @@ -{{ define "content" }} -
- {{ template "navbar" . }} -

{{ .T "Page Not Found" }}

-
-
-

- {{ .T ` - The page you were looking for does not exist. If you believe this - is a mistake then don’t hesitate to contact @onetruemangoman on - Discord or email us at %s.` - `mail@euro-cash.eu` | safe - }} -

-
-{{ end }} diff --git a/src/templates/base.html.tmpl b/src/templates/base.html.tmpl deleted file mode 100644 index 2a33cc3..0000000 --- a/src/templates/base.html.tmpl +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - {{ .T "Euro Cash" }} - - - - {{ template "content" . }} - - - diff --git a/src/templates/error.html.tmpl b/src/templates/error.html.tmpl deleted file mode 100644 index 47bff81..0000000 --- a/src/templates/error.html.tmpl +++ /dev/null @@ -1,23 +0,0 @@ -{{ define "content" }} -
- {{ template "navbar" . }} -

{{ .Code }} {{ .Msg }}

-
-
-

- {{ .T ` - If you’re seeing this page, it means that something went wrong on - our end that we need to fix. Our team has been notified of this - error, and we apologise for the inconvenience. - ` }} -

-

- {{ .T ` - If this issue persists, don’t hesitate to contact @onetruemangoman - on Discord or to email us at %s.` - `` | safe - }} -

-
-{{ end }} diff --git a/src/templates/navbar.html.tmpl b/src/templates/navbar.html.tmpl deleted file mode 100644 index 90f3cc7..0000000 --- a/src/templates/navbar.html.tmpl +++ /dev/null @@ -1,228 +0,0 @@ -{{ define "navbar" }} - -{{ end }} -- cgit v1.2.3