forked from foolin/goview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
49 lines (41 loc) · 940 Bytes
/
render.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
package goview
import (
"log"
"net/http"
)
// ViewRender struct
type ViewRender struct {
Engine *ViewEngine
Name string
Vars M
}
// Instance method
func (e *ViewEngine) Instance(name string, data ...map[string]interface{}) ViewRender {
d := make(M)
if data != nil {
d = data[0]
}
return ViewRender{
Engine: e,
Name: name,
Vars: d,
}
}
// Render method
func (r ViewRender) Render(w http.ResponseWriter) {
err := r.Engine.executeRender(w, r.Name, r.Vars)
if err != nil {
switch t := err.(type) {
case IStatusError:
// We can retrieve the status here and write out a specific
// HTTP status code.
log.Printf("HTTP %d - %s", t.Status(), t)
http.Error(w, t.Error(), t.Status())
default:
// Any error types we don't specifically look out for default
// to serving a HTTP 500
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
}
}
}