This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
renders.go
272 lines (232 loc) · 5.92 KB
/
renders.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package renders
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"github.com/lunny/tango"
"github.com/oxtoacart/bpool"
)
// Version return the middleware's version
func Version() string {
return "0.4.0329"
}
const (
ContentType = "Content-Type"
ContentLength = "Content-Length"
ContentHTML = "text/html"
ContentXHTML = "application/xhtml+xml"
defaultCharset = "UTF-8"
)
// Provides a common buffer to execute templates.
type T map[string]interface{}
func (t T) Merge(at T) T {
if len(at) <= 0 {
return t
}
for k, v := range at {
t[k] = v
}
return t
}
type Renders struct {
Options
cs string
pool *bpool.BufferPool
templates map[string]*template.Template
}
func New(options ...Options) *Renders {
opt := prepareOptions(options)
t, err := compile(opt)
if err != nil {
panic(err)
}
return &Renders{
Options: opt,
cs: prepareCharset(opt.Charset),
pool: bpool.NewBufferPool(64),
templates: t,
}
}
type IRenderer interface {
SetRenderer(*Renders, *tango.Context, func(string), func(string), func(string, io.Reader))
}
// confirm Renderer implements IRenderer
var _ IRenderer = &Renderer{}
type Renderer struct {
ctx *tango.Context
renders *Renders
before, after func(string)
afterBuf func(string, io.Reader)
compiledCharset string
Charset string
HTMLContentType string
delimsLeft, delimsRight string
}
func (r *Renderer) SetRenderer(renders *Renders, ctx *tango.Context,
before, after func(string), afterBuf func(string, io.Reader)) {
r.renders = renders
r.ctx = ctx
r.before = before
r.after = after
r.afterBuf = afterBuf
r.HTMLContentType = renders.Options.HTMLContentType
r.compiledCharset = renders.cs
r.delimsLeft = renders.Options.DelimsLeft
r.delimsRight = renders.Options.DelimsRight
}
type Before interface {
BeforeRender(string)
}
type After interface {
AfterRender(string)
}
type AfterBuf interface {
AfterRender(string, io.Reader)
}
func (r *Renders) RenderBytes(name string, bindings ...interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
err := r.Render(buf, name, bindings...)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (r *Renders) Render(w io.Writer, name string, bindings ...interface{}) error {
var binding interface{}
if len(bindings) > 0 {
binding = bindings[0]
}
if t, ok := binding.(T); ok {
binding = t.Merge(r.Options.Vars)
}
if r.Reload {
var err error
// recompile for easy development
r.templates, err = compile(r.Options)
if err != nil {
return err
}
}
buf, err := r.execute(name, binding)
if err != nil {
r.pool.Put(buf)
return err
}
// template rendered fine, write out the result
_, err = io.Copy(w, buf)
r.pool.Put(buf)
return err
}
func (r *Renders) execute(name string, binding interface{}) (*bytes.Buffer, error) {
buf := r.pool.Get()
name = alignTmplName(name)
if rt, ok := r.templates[name]; ok {
return buf, rt.ExecuteTemplate(buf, name, binding)
}
return buf, errors.New("template is not exist")
}
func (r *Renders) Handle(ctx *tango.Context) {
if action := ctx.Action(); action != nil {
if rd, ok := action.(IRenderer); ok {
var before, after func(string)
var afterBuf func(string, io.Reader)
if b, ok := action.(Before); ok {
before = b.BeforeRender
}
if a, ok := action.(After); ok {
after = a.AfterRender
}
if a2, ok := action.(AfterBuf); ok {
afterBuf = a2.AfterRender
}
rd.SetRenderer(r, ctx, before, after, afterBuf)
}
}
ctx.Next()
}
func compile(options Options) (map[string]*template.Template, error) {
return Load(options)
}
func prepareCharset(charset string) string {
if len(charset) != 0 {
return "; charset=" + charset
}
return "; charset=" + defaultCharset
}
// Render a template
// r.Render("index.html")
// r.Render("index.html", renders.T{
// "name": value,
// })
func (r *Renderer) Render(name string, bindings ...interface{}) error {
return r.StatusRender(http.StatusOK, name, bindings...)
}
// RenderBytes Will not called before & after method.
func (r *Renderer) RenderBytes(name string, binding ...interface{}) ([]byte, error) {
return r.renders.RenderBytes(name, binding...)
}
func (r *Renderer) StatusRender(status int, name string, bindings ...interface{}) error {
var binding interface{}
if len(bindings) > 0 {
binding = bindings[0]
}
if t, ok := binding.(T); ok {
binding = t.Merge(r.renders.Options.Vars)
}
if r.renders.Reload {
var err error
// recompile for easy development
r.renders.templates, err = compile(r.renders.Options)
if err != nil {
return err
}
}
buf, err := r.execute(name, binding)
if err != nil {
r.renders.pool.Put(buf)
return err
}
var cs string
if len(r.Charset) > 0 {
cs = prepareCharset(r.Charset)
} else {
cs = r.compiledCharset
}
// template rendered fine, write out the result
r.ctx.Header().Set(ContentType, r.HTMLContentType+cs)
r.ctx.WriteHeader(status)
_, err = io.Copy(r.ctx.ResponseWriter, buf)
r.renders.pool.Put(buf)
return err
}
func (r *Renderer) Template(name string) *template.Template {
return r.renders.templates[alignTmplName(name)]
}
func (r *Renderer) execute(name string, binding interface{}) (*bytes.Buffer, error) {
buf := r.renders.pool.Get()
if r.before != nil {
r.before(name)
}
if r.after != nil {
defer r.after(name)
}
name = alignTmplName(name)
if rt, ok := r.renders.templates[name]; ok {
err := rt.Delims(r.delimsLeft, r.delimsRight).ExecuteTemplate(buf, name, binding)
if err == nil && r.afterBuf != nil {
var tmpBuf = bytes.NewBuffer(buf.Bytes())
r.afterBuf(name, tmpBuf)
}
return buf, err
}
if r.afterBuf != nil {
r.afterBuf(name, nil)
}
return buf, fmt.Errorf("template %s is not exist", name)
}