Repository navigation
Expand file tree
/
Copy pathview.go
More file actions
117 lines (101 loc) 路 2.71 KB
/
Copy pathview.go
File metadata and controls
117 lines (101 loc) 路 2.71 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Package view provides a superset of the standard html/template package.
It allows to keep global data for templates and parse strings, files and directories (also recursively).
*/
package view
import (
"html/template"
"io"
"os"
"path/filepath"
)
// View contains templates, global data and functions.
type View struct {
tmpl *template.Template
data Data
}
// Data is a map of data passed to a view template.
type Data map[string]interface{}
// Funcs is a map of functions usable inside view templates.
type Funcs map[string]interface{}
// New init a view unit.
func New() *View {
v := &View{tmpl: template.New("main")}
v.Funcs(allHelpers)
return v
}
// Data adds global data for view templates.
func (v *View) Data(data Data) *View {
if v.data == nil {
v.data = data
} else {
for n, f := range data {
v.data[n] = f
}
}
return v
}
// Funcs adds functions for view templates.
func (v *View) Funcs(funcs Funcs) *View {
v.tmpl.Funcs(template.FuncMap(funcs))
return v
}
func templateMust(t *template.Template, err error) *template.Template {
if err != nil {
panic("view: " + err.Error())
}
return t
}
// Parse parses text and associates the resulting templates with view.
func (v *View) Parse(text string) *View {
v.tmpl = templateMust(v.tmpl.Parse(text))
return v
}
// ParseFiles parses the named files and associates the resulting templates with view.
func (v *View) ParseFiles(filenames ...string) *View {
v.tmpl = templateMust(v.tmpl.ParseFiles(filenames...))
return v
}
// ParseGlob parses the files identified by the pattern and associates the resulting templates with view.
func (v *View) ParseGlob(pattern string) *View {
v.tmpl = templateMust(v.tmpl.ParseGlob(pattern))
return v
}
// ParseDir parses recursively all files in directory and associates the resulting templates with view.
func (v *View) ParseDir(dirpath string) *View {
if err := filepath.Walk(dirpath, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
_, err = v.tmpl.ParseFiles(path)
return err
}); err != nil {
panic("view: " + err.Error())
}
return v
}
// setExecuteData populates referenced data mapped with the view global data.
func (v *View) setExecuteData(data *Data) {
if v.data != nil {
if data == nil {
*data = v.data
} else {
for n, f := range v.data {
(*data)[n] = f
}
}
}
}
// Execute writes w with the main template.
func (v *View) Execute(w io.Writer, data Data) error {
v.setExecuteData(&data)
return v.tmpl.Execute(w, data)
}
// ExecuteTemplate writes w with the named template.
func (v *View) ExecuteTemplate(w io.Writer, name string, data Data) error {
v.setExecuteData(&data)
return v.tmpl.ExecuteTemplate(w, name, data)
}