-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviews.go
70 lines (57 loc) · 1.44 KB
/
views.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
package app
import (
"html/template"
"os"
"strings"
"github.com/gowww/static"
"github.com/gowww/view"
)
const (
viewsDir = "views"
staticDir = "static"
)
var (
staticHandler = static.Handle("/"+staticDir+"/", staticDir)
views = view.New()
)
// ViewData represents data for a view rendering.
type ViewData map[string]interface{}
// ViewFuncs is a map of functions passed to all view renderings.
type ViewFuncs map[string]interface{}
// GlobalViewData adds global data for view templates.
func GlobalViewData(data ViewData) {
views.Data(view.Data(data))
}
// GlobalViewFuncs adds functions for view templates.
func GlobalViewFuncs(funcs ViewFuncs) {
views.Funcs(view.Funcs(funcs))
}
func initViews() {
if _, err := os.Stat(viewsDir); err != nil { // viewsDir not found: nothing to parse.
return
}
GlobalViewData(ViewData{
"envProduction": production,
})
GlobalViewFuncs(ViewFuncs{
"asset": func(path string) string {
return staticHandler.Hash(path)
},
"script": func(src string) template.HTML {
return view.HelperScript(staticHandler.Hash("scripts/" + strings.TrimPrefix(src, "/")))
},
"style": func(href string) template.HTML {
return view.HelperStyle(staticHandler.Hash("styles/" + strings.TrimPrefix(href, "/")))
},
})
views.ParseDir(viewsDir)
}
func mergeViewData(dd []ViewData) view.Data {
data := make(view.Data, len(dd))
for _, d := range dd {
for k, v := range d {
data[k] = v
}
}
return data
}