-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
36 lines (27 loc) · 869 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
package main
import (
"net/http"
"github.com/kissen/fmajor/templates"
)
// Render out page to (w, r) with parameters vs.
//
// If this function encounters errors, it calls http.Error to forward
// that error to the client. It does not use our fancy Error function
// because that function uses Render and I would prefer to avoid infinite
// recursion.
func Render(w http.ResponseWriter, r *http.Request, status int, page string, vs map[string]interface{}) {
// Set some flags used by all templates.
if vs == nil {
vs = make(map[string]interface{})
}
if authed, err := IsAuthorized(r); err == nil {
vs["IsAuthorized"] = authed
}
// Set status code.
w.WriteHeader(status)
// Render out the page to the HTTP writer.
if err := templates.Render(w, r, page, vs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}