-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.go
56 lines (49 loc) · 954 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
50
51
52
53
54
55
56
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
"text/template"
)
func renderJSON(w http.ResponseWriter, data interface{}) {
var j []byte
var err error
if os.Getenv("DEV") == "1" {
j, err = json.MarshalIndent(data, "", " ")
} else {
j, err = json.Marshal(data)
}
if err != nil {
log.Println(err)
httpGenericErr(w)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf8")
w.Write(j)
}
func renderHTML(w http.ResponseWriter, data interface{}, tmplPathName string) {
t, err := template.ParseFiles(
"tmpl/layout/app.html",
"tmpl/layout/pagination.html",
"tmpl/"+tmplPathName+".html",
)
if err != nil {
log.Println(err)
httpGenericErr(w)
return
}
// Avoid partial rendering
b := &bytes.Buffer{}
err = t.Execute(b, data)
if err != nil {
log.Println(err)
httpGenericErr(w)
return
}
_, err = b.WriteTo(w)
if err != nil {
log.Fatalf("renderHTML: %s\n", err)
}
}