-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.go
More file actions
51 lines (42 loc) · 831 Bytes
/
page.go
File metadata and controls
51 lines (42 loc) · 831 Bytes
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
package htmlview
import (
"html/template"
"net/http"
)
type Page struct {
tmpl *template.Template
args map[string]interface{}
header http.Header
StatusCode int
}
func (p *Page) Set(name string, value interface{}) *Page {
if p.args == nil {
p.args = map[string]interface{}{}
}
p.args[name] = value
return p
}
func (p *Page) Get(name string) interface{} {
return p.args[name]
}
func (p *Page) Header() http.Header {
if p.header == nil {
p.header = http.Header{}
}
return p.header
}
func (p *Page) Render(w http.ResponseWriter) error {
// Copy headers
if len(p.header) > 0 {
hdr := w.Header()
for k, v := range p.header {
hdr[k] = v
}
}
// Set the return code
if p.StatusCode != 0 {
w.WriteHeader(p.StatusCode)
}
// Execute the template
return p.tmpl.Execute(w, p.args)
}