aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/templates.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/templates.go')
-rw-r--r--src/templates.go80
1 files changed, 67 insertions, 13 deletions
diff --git a/src/templates.go b/src/templates.go
index 353c755..d6b82b8 100644
--- a/src/templates.go
+++ b/src/templates.go
@@ -1,7 +1,7 @@
package app
import (
- "fmt"
+ "bytes"
"html/template"
"io/fs"
"log"
@@ -15,7 +15,9 @@ import (
)
type templateData struct {
+ Debugp bool
Printer i18n.Printer
+ Printers map[string]i18n.Printer
Code, Type string
Mintages dbx.MintageData
Countries []country
@@ -26,16 +28,17 @@ var (
errorTmpl *template.Template
templates map[string]*template.Template
funcmap = map[string]any{
- "locales": i18n.Locales,
- "safe": asHTML,
- "sprintf": fmt.Sprintf,
- "toUpper": strings.ToUpper,
- "tuple": templateMakeTuple,
- "map": templateMakeMap,
+ "ifElse": ifElse,
+ "locales": i18n.Locales,
+ "map": templateMakeMap,
+ "safe": asHTML,
+ "toUpper": strings.ToUpper,
+ "tuple": templateMakeTuple,
+ "makeHeaderWithTranslations": makeHeaderWithTranslations,
}
)
-func BuildTemplates(dir fs.FS, debugp bool) {
+func BuildTemplates(dir fs.FS) {
ents := Try2(fs.ReadDir(dir, "."))
notFoundTmpl = buildTemplate(dir, "-404")
errorTmpl = buildTemplate(dir, "-error")
@@ -44,7 +47,7 @@ func BuildTemplates(dir fs.FS, debugp bool) {
for _, e := range ents {
name := e.Name()
buildAndSetTemplate(dir, name)
- if debugp {
+ if Debugp {
go watch.FileFS(dir, name, func() {
defer func() {
if p := recover(); p != nil {
@@ -76,10 +79,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 {
@@ -107,6 +109,58 @@ 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
+ }
+ return y
+}
+
+func makeHeaderWithTranslations(tag string, text string,
+ translations ...[]any) template.HTML {
+ var bob strings.Builder
+ bob.WriteByte('<')
+ bob.WriteString(tag)
+ bob.WriteByte('>')
+ bob.WriteString(text)
+
+ /* TODO: Assert that the pairs are [2]string */
+ for _, pair := range translations {
+ if text == pair[1] {
+ continue
+ }
+ bob.WriteString(`<br><span class="translation"`)
+ if pair[0].(string) != "" {
+ bob.WriteString(` lang="`)
+ bob.WriteString(pair[0].(string))
+ bob.WriteString(`">`)
+ } else {
+ bob.WriteByte('>')
+ }
+ bob.WriteString(pair[1].(string))
+ bob.WriteString("</span>")
+ }
+
+ bob.WriteString("</")
+ bob.WriteString(tag)
+ bob.WriteByte('>')
+ return template.HTML(bob.String())
+}
+
func (td templateData) Get(fmt string, args ...map[string]any) template.HTML {
return template.HTML(td.Printer.Get(fmt, args...))
}