-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtemplates.go
More file actions
32 lines (27 loc) · 791 Bytes
/
templates.go
File metadata and controls
32 lines (27 loc) · 791 Bytes
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 main
import (
"html/template"
"net/http"
"strings"
)
//go:generate go-bindata views/...
var templates = template.New("")
func init() {
for _, name := range AssetNames() {
bytes, _ := Asset(name)
logger.Debugf("registering template: %s", templateName(name))
templates.New(templateName(name)).Parse(string(bytes))
}
}
func templateName(path string) string {
dot := strings.LastIndex(path, ".")
slash := strings.LastIndex(path, "/")
return path[slash+1 : dot]
}
func RenderTemplate(w http.ResponseWriter, name string, data interface{}) {
if err := templates.ExecuteTemplate(w, name, data); err != nil {
logger.WithField("template", name).WithField("error", err).Warnf("error while rendering template")
w.WriteHeader(http.StatusInternalServerError)
return
}
}