/
view_response.go
53 lines (42 loc) · 970 Bytes
/
view_response.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
package response
import (
"bytes"
"html/template"
"net/http"
)
type ViewResponse struct {
basicResponse
templateName string
*template.Template
data interface{}
}
func (r *ViewResponse) GetBodyBytes() *bytes.Buffer {
if nil == r.Template {
panic("Failed to render view, no template object set for template " + r.templateName)
}
result := bytes.NewBuffer(make([]byte, 0))
err := r.Template.ExecuteTemplate(result, r.templateName, r.data)
if err != nil {
panic(err)
}
return result
}
func (r *ViewResponse) SetTemplate(tpl *template.Template) {
r.Template = tpl
}
func (r *ViewResponse) SetData(data interface{}) {
r.data = data
}
//--------------------
func NewViewResponse(templateName string) *ViewResponse {
r := &ViewResponse{
basicResponse: basicResponse{
httpStatus: http.StatusOK,
headers: make(http.Header),
},
templateName: templateName,
data: nil,
}
r.HeaderSet("Content-Type", "text/html")
return r
}