/
templates.go
61 lines (58 loc) · 1.35 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
package trojanTechTickets
import (
"encoding/json"
"html/template"
"strconv"
"time"
)
var templates = template.Must(template.New("").Funcs(TemplateHelpers).ParseGlob("views/*.html"))
var TemplateHelpers = template.FuncMap{
"gradeString": func(grade int) string {
switch grade {
case 9:
return "Freshman"
case 10:
return "Sophomore"
case 11:
return "Junior"
case 12:
return "Senior"
default:
return strconv.Itoa(grade)
}
},
"json": func(v interface{}) (string, error) {
b, err := json.Marshal(v)
return string(b), err
},
"timeFormat": func(format string, time time.Time) string {
return time.Format(format)
},
"timeIn": func(tz string, t time.Time) (time.Time, error) {
location, err := time.LoadLocation(tz)
if err != nil {
return time.Time{}, err
}
return t.In(location), nil
},
"truncate": func(maxLen int, str string) string {
if len(str) < maxLen {
return str
}
return str[:maxLen-3] + "..."
},
"workerName": func(email, workersJSON string) string {
// TODO A less completely *stupid* way of passing around the workers
// list than putting it to JSON and back.
workers := make([]Worker, 0, 10)
if err := json.Unmarshal([]byte(workersJSON), &workers); err != nil {
return email
}
for _, worker := range workers {
if worker.Email == email {
return worker.Name
}
}
return email
},
}