/
templates.go
65 lines (52 loc) · 1.49 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
64
65
package main
import (
"html/template"
"path/filepath"
"time"
)
type templateData struct {
Checklist map[string]*Check
Sandbox Sandbox
Configuration Configuration
}
func newTemplateCache(dir string) (map[string]*template.Template, error) {
// Initialize new map to act as cache
cache := map[string]*template.Template{}
// Get all 'page' templates for the application
pages, err := filepath.Glob(filepath.Join(dir, "*.page.tmpl"))
if err != nil {
return nil, err
}
// Loop through the pages one-by-one.
for _, page := range pages {
// Extract file name from the full file path
name := filepath.Base(page)
// Parse page template file in to template set
ts, err := template.New(name).Funcs(functions).ParseFiles(page)
if err != nil {
return nil, err
}
// Add any 'layout' templates to thetemplate set
ts, err = ts.ParseGlob(filepath.Join(dir, "*.layout.tmpl"))
if err != nil {
return nil, err
}
// Add any 'partial' templates to the template set
ts, err = ts.ParseGlob(filepath.Join(dir, "*.partial.tmpl"))
if err != nil {
return nil, err
}
// Add template set to the cache, using name of the page as key
cache[name] = ts
}
return cache, nil
}
// Converts a unix timestamp to human readable datetime
func humanDate(t int64) string {
current := time.Unix(t, int64(0))
return current.Format("2006-01-02 15:04:05")
}
// Initialize a template.FuncMap object and store it in a global variable.
var functions = template.FuncMap{
"humanDate": humanDate,
}