-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
41 lines (34 loc) · 924 Bytes
/
template.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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"path/filepath"
)
// parseTemplate applies a given file to the body of the base template.
func parseTemplate(filename string) *htmlTemplate {
tmpl := template.Must(template.ParseFiles("templates/base.html"))
// Put the named file into a template called "body"
path := filepath.Join("templates", filename)
b, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Errorf("could not read template: %v", err))
}
template.Must(tmpl.New("body").Parse(string(b)))
return &htmlTemplate{tmpl.Lookup("base.html")}
}
type htmlTemplate struct {
t *template.Template
}
func (tmpl *htmlTemplate) Execute(w http.ResponseWriter, r *http.Request, data interface{}) *apiError {
d := struct {
Data interface{}
}{
Data: data,
}
if err := tmpl.t.Execute(w, d); err != nil {
return apiErrorf(err, "couldn't write template: %v")
}
return nil
}