-
Notifications
You must be signed in to change notification settings - Fork 4
/
templates.go
63 lines (54 loc) · 1.29 KB
/
templates.go
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
57
58
59
60
61
62
63
package main
import (
"html/template"
"io"
"io/ioutil"
"log"
"path/filepath"
)
// TemplateInput is the input to a rendered Template. Body should name a
// template-file. Data will be provided to the Body-Template.
type TemplateInput struct {
Title string
Body string
Data interface{}
}
var (
parsedTemplates = make(map[string]*template.Template)
)
func init() {
layout, err := ioutil.ReadFile("templates/layout.html")
if err != nil {
log.Fatal("Could not read layout:", err)
}
files, err := filepath.Glob("templates/*")
if err != nil {
log.Fatal("Could not glob templates:", err)
}
for _, f := range files {
if filepath.Base(f) == "layout.html" {
continue
}
// Skip hidden files
if filepath.Base(f)[0] == '.' {
continue
}
content, err := ioutil.ReadFile(f)
if err != nil {
log.Fatalf("Could not read %q: %v", f, err)
}
t := template.New("page")
t.Funcs(map[string]interface{}{
"toEuros": func(x int) float64 {
return float64(x) / 100
},
})
t = template.Must(t.Parse(string(layout)))
template.Must(t.New("content").Parse(string(content)))
parsedTemplates[filepath.Base(f)] = t
}
}
// ExecuteTemplate executes a template to w.
func ExecuteTemplate(w io.Writer, data TemplateInput) error {
return parsedTemplates[data.Body].Execute(w, data)
}