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
|
package renderer
import (
"html/template"
"net/http"
"path"
"path/filepath"
)
// Renderer represents html template renderer
type Renderer struct {
templates map[string]*template.Template
}
// New Renderer
func New(gpath string) (*Renderer, error) {
t := &Renderer{
templates: make(map[string]*template.Template),
}
includes, err := filepath.Glob(path.Join(gpath, "includes/*.gohtml"))
if err != nil {
return nil, err
}
pages, err := filepath.Glob(path.Join(gpath, "pages/*.gohtml"))
if err != nil {
return nil, err
}
for _, p := range pages {
tpls := append(includes, p)
tname := path.Base(p)
t.templates[tname] = template.Must(template.New(tname).ParseFiles(tpls...))
}
return t, nil
}
func (r *Renderer) Render(tpl string, data Map, rw http.ResponseWriter) error {
rw.Header().Set("Content-Type", "text/html")
return r.templates[tpl].Execute(rw, data)
}
type Map map[string]interface{}
|