summaryrefslogtreecommitdiffhomepage
path: root/src/templates.go
blob: 839a6fbb4e7bf4ce2bc9a2904d7ebc6210024878 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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...)
}