-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.go
77 lines (66 loc) · 2.08 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
68
69
70
71
72
73
74
75
76
77
// Package pongo2gin is a template renderer that can be used with the Gin
// web framework https://github.com/gin-gonic/gin it uses the Pongo2 template
// library https://github.com/flosch/pongo2
package pongo2gin
import (
"net/http"
"path"
"github.com/flosch/pongo2"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
)
// RenderOptions is used to configure the renderer.
type RenderOptions struct {
TemplateDir string
ContentType string
}
// Pongo2Render is a custom Gin template renderer using Pongo2.
type Pongo2Render struct {
Options *RenderOptions
Template *pongo2.Template
Context pongo2.Context
}
// New creates a new Pongo2Render instance with custom Options.
func New(options RenderOptions) *Pongo2Render {
return &Pongo2Render{
Options: &options,
}
}
// Default creates a Pongo2Render instance with default options.
func Default() *Pongo2Render {
return New(RenderOptions{
TemplateDir: "templates",
ContentType: "text/html; charset=utf-8",
})
}
// Instance should return a new Pongo2Render struct per request and prepare
// the template by either loading it from disk or using pongo2's cache.
func (p Pongo2Render) Instance(name string, data interface{}) render.Render {
var template *pongo2.Template
filename := path.Join(p.Options.TemplateDir, name)
// always read template files from disk if in debug mode, use cache otherwise.
if gin.Mode() == "debug" {
template = pongo2.Must(pongo2.FromFile(filename))
} else {
template = pongo2.Must(pongo2.FromCache(filename))
}
return Pongo2Render{
Template: template,
Context: data.(pongo2.Context),
Options: p.Options,
}
}
// Render should render the template to the response.
func (p Pongo2Render) Render(w http.ResponseWriter) error {
p.WriteContentType(w)
err := p.Template.ExecuteWriter(p.Context, w)
return err
}
// WriteContentType should add the Content-Type header to the response
// when not set yet.
func (p Pongo2Render) WriteContentType(w http.ResponseWriter) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = []string{p.Options.ContentType}
}
}