/
render.go
32 lines (29 loc) · 915 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
package server
import (
"net/http"
"path"
"github.com/flosch/pongo2"
"github.com/gorilla/context"
"github.com/nathan-osman/informas/db"
)
// render loads the specified template, injects the provided context, and
// renders it directly to the response.
func (s *Server) render(w http.ResponseWriter, r *http.Request, templateName string, ctx pongo2.Context) {
t, err := pongo2.FromFile(path.Join(s.templateDir, templateName))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx["request"] = r
ctx["alerts"] = s.getAlerts(w, r)
ctx["current_user"] = context.Get(r, contextCurrentUser).(*db.User)
ctx["site_title"] = s.config.GetString(configSiteTitle)
b, err := t.ExecuteBytes(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write(b)
}