/
render.go
77 lines (63 loc) · 1.92 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package web
import (
"fmt"
"html/template"
"net/http"
"path/filepath"
"github.com/oxtoacart/bpool"
)
var (
templates map[string]*template.Template
bufpool *bpool.BufferPool
loaded = false
)
// renderTemplate is a wrapper around template.ExecuteTemplate.
// It writes into a bytes.Buffer before writing to the http.ResponseWriter to catch
// any errors resulting from populating the template.
func renderTemplate(w http.ResponseWriter, name string, data map[string]interface{}) error {
loadTemplates()
// Ensure the template exists in the map.
tmpl, ok := templates[name]
if !ok {
return fmt.Errorf("The template %s does not exist", name)
}
// Create a buffer to temporarily write to and check if any errors were encountered.
buf := bufpool.Get()
defer bufpool.Put(buf)
err := tmpl.ExecuteTemplate(buf, "base", data)
if err != nil {
return err
}
// The X-Frame-Options HTTP response header can be used to indicate whether
// or not a browser should be allowed to render a page in a <frame>,
// <iframe> or <object> . Sites can use this to avoid clickjacking attacks,
// by ensuring that their content is not embedded into other sites.
w.Header().Set("X-Frame-Options", "deny")
// Set the header and write the buffer to the http.ResponseWriter
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w)
return nil
}
func loadTemplates() {
if loaded {
return
}
templates = make(map[string]*template.Template)
bufpool = bpool.NewBufferPool(64)
layoutTemplates := map[string][]string{
"web/layouts/outside.html": {
"./web/includes/register.html",
"./web/includes/login.html",
},
"web/layouts/inside.html": {
"./web/includes/authorize.html",
},
}
for layout, includes := range layoutTemplates {
for _, include := range includes {
files := []string{include, layout}
templates[filepath.Base(include)] = template.Must(template.ParseFiles(files...))
}
}
loaded = true
}