-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
82 lines (74 loc) · 1.83 KB
/
template.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"html/template"
"net/http"
"path/filepath"
)
func (h *Handler) loadTemplates() {
h.tpls = make(map[string]*template.Template)
mainTmpl := template.New("main")
mainTmpl.Funcs(template.FuncMap{
"formatTime": formatTime,
"formatDate": formatDate,
"add": add,
"sub": sub,
"dashToSlash": dashToSlash,
})
layoutFiles, err := filepath.Glob("templates/layouts/*.htm")
if err != nil {
panic(err)
}
customFiles, err := filepath.Glob("templates/custom/*.htm")
if err != nil {
panic(err)
}
includeFiles, err := filepath.Glob("templates/*.htm")
if err != nil {
panic(err)
}
files := make([]string, 1, len(layoutFiles)+len(customFiles)+1)
files = append(files, layoutFiles...)
files = append(files, customFiles...)
for _, file := range includeFiles {
fileName := filepath.Base(file)
files[0] = file
tpl := template.Must(mainTmpl.Clone())
h.tpls[fileName] = template.Must(tpl.ParseFiles(files...))
}
}
func (h Handler) HTML(w http.ResponseWriter, r *http.Request, page string, data interface{}) {
acct, ok := r.Context().Value(KeyAccount).(Account)
pageData := struct {
Data interface{}
Login bool
Account
Config Config
Message string
}{
data,
ok,
acct,
h.config,
"",
}
if msg, ok := r.Context().Value(KeyMessage).(string); ok {
pageData.Message = msg
}
if tpl, ok := h.tpls[page]; ok {
if err := tpl.ExecuteTemplate(w, "main", pageData); err != nil {
http.Error(w, err.Error(), 500)
}
} else {
http.Error(w, "cannot find templates", 500)
}
}
func (h Handler) page(path string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.HTML(w, r, path, nil)
}
}
func (h Handler) message(w http.ResponseWriter, r *http.Request, title, msg string) {
h.HTML(w, r, "message.htm", struct {
Title, Message string
}{title, msg})
}