-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrender.go
67 lines (56 loc) · 1.27 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
package render
import (
"net/http"
"path"
"github.com/flosch/pongo2"
"github.com/gin-gonic/gin/render"
)
type (
PongoProduction struct {
Templates map[string]*pongo2.Template
Path string
}
PongoDebug struct {
Path string
}
Pongo struct {
Template *pongo2.Template
Name string
Data interface{}
}
Context pongo2.Context
)
func NewProduction(path string) *PongoProduction {
return &PongoProduction{map[string]*pongo2.Template{}, path}
}
func NewDebug(path string) *PongoDebug {
return &PongoDebug{path}
}
func (p PongoProduction) Instance(name string, data interface{}) render.Render {
var t *pongo2.Template
if tmpl, ok := p.Templates[name]; ok {
t = tmpl
} else {
tmpl := pongo2.Must(pongo2.FromFile(path.Join(p.Path, name)))
p.Templates[name] = tmpl
t = tmpl
}
return Pongo{
Template: t,
Name: name,
Data: data,
}
}
func (p PongoDebug) Instance(name string, data interface{}) render.Render {
t := pongo2.Must(pongo2.FromFile(path.Join(p.Path, name)))
return Pongo{
Template: t,
Name: name,
Data: data,
}
}
func (p Pongo) Render(w http.ResponseWriter) error {
ctx := pongo2.Context(p.Data.(Context))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
return p.Template.ExecuteWriter(ctx, w)
}