aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2025-07-29 21:05:37 +0200
committerThomas Voss <mail@thomasvoss.com> 2025-07-29 21:05:37 +0200
commit23adeec51e0960d650ea933eaeebef425ac474b8 (patch)
tree2d54c9b48db5049501d6e86a3f1ef61ef5e4f98a /src
parent22cee093fc4ec9c36892a0edd9f00bcb1d64ee16 (diff)
Add conditional includes
Diffstat (limited to 'src')
-rw-r--r--src/templates.go22
-rw-r--r--src/templates/-base.html.tmpl1
2 files changed, 19 insertions, 4 deletions
diff --git a/src/templates.go b/src/templates.go
index 94d1acb..e3a0465 100644
--- a/src/templates.go
+++ b/src/templates.go
@@ -1,6 +1,7 @@
package app
import (
+ "bytes"
"html/template"
"io/fs"
"log"
@@ -75,10 +76,9 @@ func buildTemplate(dir fs.FS, name string) *template.Template {
for i, s := range names {
names[i] = s + ".html.tmpl"
}
- return template.Must(template.
- New("-base.html.tmpl").
- Funcs(funcmap).
- ParseFS(dir, names[:]...))
+ t := template.New("-base.html.tmpl").Funcs(funcmap)
+ t = t.Funcs(includeIfExists(t))
+ return template.Must(t.ParseFS(dir, names[:]...))
}
func asHTML(s string) template.HTML {
@@ -106,6 +106,20 @@ 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 (td templateData) Get(fmt string, args ...map[string]any) template.HTML {
return template.HTML(td.Printer.Get(fmt, args...))
}
diff --git a/src/templates/-base.html.tmpl b/src/templates/-base.html.tmpl
index ba2ef7d..46b81eb 100644
--- a/src/templates/-base.html.tmpl
+++ b/src/templates/-base.html.tmpl
@@ -31,6 +31,7 @@
setTheme(validate(localStorage.getItem("theme")));
});
</script>
+ {{ includeIfExists "header" . }}
</head>
<body>
{{ template "content" . }}