From db7dc92fc8726a0a89b26fbac5480d9e3847a55f Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 2 Aug 2025 00:24:54 +0200 Subject: Change how we do headers --- src/templates.go | 22 +++------------- src/templates/-404.html.tmpl | 4 +++ src/templates/-base.html.tmpl | 3 ++- src/templates/-error.html.tmpl | 4 +++ src/templates/-navbar.html.tmpl | 40 ++++++++++++++++++++++++++++++ src/templates/about.html.tmpl | 4 +++ src/templates/banknotes-codes.html.tmpl | 4 +++ src/templates/banknotes.html.tmpl | 6 ++++- src/templates/coins-designs-ad.html.tmpl | 6 ++++- src/templates/coins-designs-at.html.tmpl | 6 ++++- src/templates/coins-designs-be.html.tmpl | 6 ++++- src/templates/coins-designs-de.html.tmpl | 6 ++++- src/templates/coins-designs-ee.html.tmpl | 6 ++++- src/templates/coins-designs-hr.html.tmpl | 6 ++++- src/templates/coins-designs-nl.html.tmpl | 6 ++++- src/templates/coins-designs.html.tmpl | 2 ++ src/templates/coins-mintages.html.tmpl | 4 +++ src/templates/coins.html.tmpl | 4 +++ src/templates/collecting-crh.html.tmpl | 4 +++ src/templates/collecting-storage.html.tmpl | 6 ++++- src/templates/collecting-vending.html.tmpl | 4 +++ src/templates/collecting.html.tmpl | 4 +++ src/templates/index.html.tmpl | 5 ++++ src/templates/jargon.html.tmpl | 6 ++++- src/templates/language.html.tmpl | 2 ++ 25 files changed, 141 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/templates.go b/src/templates.go index a7d7f2a..a3bc52d 100644 --- a/src/templates.go +++ b/src/templates.go @@ -1,7 +1,6 @@ package app import ( - "bytes" "html/template" "io/fs" "log" @@ -82,9 +81,10 @@ func buildTemplate(dir fs.FS, name string) *template.Template { for i, s := range names { names[i] = s + ".html.tmpl" } - t := template.New("-base.html.tmpl").Funcs(funcmap) - t = t.Funcs(includeIfExists(t)) - return template.Must(t.ParseFS(dir, names[:]...)) + return template.Must(template. + New("-base.html.tmpl"). + Funcs(funcmap). + ParseFS(dir, names[:]...)) } func asHTML(s string) template.HTML { @@ -112,20 +112,6 @@ func templateMakeMap(args ...any) map[string]any { return m } -func includeIfExists(tmpl *template.Template) template.FuncMap { - return template.FuncMap{ - "includeIfExists": func(name string, data any) (template.HTML, error) { - t := tmpl.Lookup(name) - if t == nil { - return "", nil - } - var buf bytes.Buffer - err := t.Execute(&buf, data) - return template.HTML(buf.String()), err - }, - } -} - func ifElse(b bool, x, y any) any { if b { return x diff --git a/src/templates/-404.html.tmpl b/src/templates/-404.html.tmpl index 03602e1..b149777 100644 --- a/src/templates/-404.html.tmpl +++ b/src/templates/-404.html.tmpl @@ -1,3 +1,7 @@ +{{ define "header" }} +{{ template "header-navbar" . }} +{{ end }} + {{ define "content" }}
{{ template "navbar" . }} diff --git a/src/templates/-base.html.tmpl b/src/templates/-base.html.tmpl index 42ec216..62f3301 100644 --- a/src/templates/-base.html.tmpl +++ b/src/templates/-base.html.tmpl @@ -33,7 +33,8 @@ }); })(); - {{ includeIfExists "header" . }} + + {{ template "header" . }} {{ template "content" . }} diff --git a/src/templates/-error.html.tmpl b/src/templates/-error.html.tmpl index a18ec3e..198318d 100644 --- a/src/templates/-error.html.tmpl +++ b/src/templates/-error.html.tmpl @@ -1,3 +1,7 @@ +{{ define "header" }} +{{ template "header-navbar" . }} +{{ end }} + {{ define "content" }}
{{ template "navbar" . }} diff --git a/src/templates/-navbar.html.tmpl b/src/templates/-navbar.html.tmpl index 442de5b..33484b8 100644 --- a/src/templates/-navbar.html.tmpl +++ b/src/templates/-navbar.html.tmpl @@ -1,3 +1,43 @@ +{{ define "header-navbar" }} + +{{ end }} + {{ define "navbar" }}