summaryrefslogtreecommitdiffhomepage
path: root/src/templates.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/templates.go')
-rw-r--r--src/templates.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/templates.go b/src/templates.go
new file mode 100644
index 0000000..839a6fb
--- /dev/null
+++ b/src/templates.go
@@ -0,0 +1,56 @@
+package src
+
+import (
+ "embed"
+ "html/template"
+ "strings"
+
+ "git.thomasvoss.com/euro-cash.eu/src/mintage"
+)
+
+type templateData struct {
+ Printer Printer
+ Code, Type string
+ Mintages mintage.Data
+ Countries []country
+}
+
+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"),
+ "/language": buildTemplate("language"),
+ }
+ funcmap = map[string]any{
+ "safe": asHTML,
+ "locales": locales,
+ "toUpper": strings.ToUpper,
+ }
+)
+
+func buildTemplate(names ...string) *template.Template {
+ names = append([]string{"base", "navbar"}, names...)
+ for i, s := range names {
+ names[i] = "templates/" + s + ".html.tmpl"
+ }
+ return template.Must(template.
+ New("base.html.tmpl").
+ Funcs(funcmap).
+ ParseFS(templateFS, names...))
+}
+
+func asHTML(s string) template.HTML {
+ return template.HTML(s)
+}
+
+func locales() []locale {
+ return Locales[:]
+}
+
+func (td templateData) T(fmt string, args ...any) string {
+ return td.Printer.T(fmt, args...)
+}