forked from kataras/iris
/
render.go
237 lines (210 loc) · 7.71 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
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
package config
import (
"html/template"
"github.com/flosch/pongo2"
"github.com/imdario/mergo"
)
const (
// NoEngine is a Template's config for engine type
// when use this, the templates are disabled
NoEngine EngineType = -1
// HTMLEngine is a Template's config for engine type
// when use this, the templates are html/template
HTMLEngine EngineType = 0
// PongoEngine is a Template's config for engine type
// when use this, the templates are flosch/pongo2
PongoEngine EngineType = 1
// MarkdownEngine is a Template's config for engine type
// when use this, the templates are .md files
MarkdownEngine EngineType = 2
// JadeEngine is a Template's config for engine type
// when use this, the templates are joker/jade
JadeEngine EngineType = 3
// AmberEngine is a Template's config for engine type
// when use this, the templates are eknkc/amber
AmberEngine EngineType = 4
// DefaultEngine is the HTMLEngine
DefaultEngine EngineType = HTMLEngine
// NoLayout to disable layout for a particular template file
NoLayout = "@.|.@iris_no_layout@.|.@"
// TemplateLayoutContextKey is the name of the user values which can be used to set a template layout from a middleware and override the parent's
TemplateLayoutContextKey = "templateLayout"
)
var (
// Charset character encoding.
Charset = "UTF-8"
)
type (
// Rest is a struct for specifying configuration options for the rest.Render object.
Rest struct {
// Appends the given character set to the Content-Type header. Default is "UTF-8".
Charset string
// Gzip enable it if you want to render with gzip compression. Default is false
Gzip bool
// Outputs human readable JSON.
IndentJSON bool
// Outputs human readable XML. Default is false.
IndentXML bool
// Prefixes the JSON output with the given bytes. Default is false.
PrefixJSON []byte
// Prefixes the XML output with the given bytes.
PrefixXML []byte
// Unescape HTML characters "&<>" to their original values. Default is false.
UnEscapeHTML bool
// Streams JSON responses instead of marshalling prior to sending. Default is false.
StreamingJSON bool
// Disables automatic rendering of http.StatusInternalServerError when an error occurs. Default is false.
DisableHTTPErrorRendering bool
// MarkdownSanitize sanitizes the markdown. Default is false.
MarkdownSanitize bool
}
// EngineType is the type of template engine
EngineType int8
// Template the configs for templates (template/view engines)
// contains common configs for all template engines
Template struct {
// Engine the type of template engine
// default is DefaultEngine (HTMLEngine)
Engine EngineType
// Gzip enable gzip compression
// default is false
Gzip bool
// Minify minifies the html result,
// Note: according to this https://github.com/tdewolff/minify/issues/35, also it removes some </tags> when minify on writer, remove this from Iris until fix.
// Default is false
//Minify bool
// IsDevelopment re-builds the templates on each request
// default is false
IsDevelopment bool
// Directory the system path which the templates live
// default is ./templates
Directory string
// Extensions the allowed file extension
// default is []string{".html"}
Extensions []string
// ContentType is the Content-Type response header
// default is text/html but you can change if if needed
ContentType string
// Charset the charset, default is UTF-8
Charset string
// Asset is a func which returns bytes, use it to load the templates by binary
Asset func(name string) ([]byte, error)
// AssetNames should returns the template filenames, look Asset
AssetNames func() []string
// Layout the template file ( with its extension) which is the mother of all
// use it to have it as a root file, and include others with {{ yield }}, refer the docs
Layout string
// HTMLTemplate contains specific configs for HTMLTemplate standard html/template
HTMLTemplate HTMLTemplate
// Jade contains specific configs for Jade
Jade Jade
// Pongo contains specific configs for pongo2
Pongo Pongo
// Markdown contains specific configs for markdown
// this doesn't supports Layout & binding context
Markdown Markdown
// Amber contains specific configs for amber
Amber Amber
}
// HTMLTemplate the configs for HTMLEngine
HTMLTemplate struct {
// RequirePartials default is false
RequirePartials bool
// Delims
// Left delimeter, default is {{
Left string
// Right delimeter, default is }}
Right string
// Funcs like html/template
Funcs map[string]interface{}
// LayoutFuncs like html/template
// the difference from Funcs is that these funcs
// can be used inside a layout and can override the predefined (yield,partial...) or add more custom funcs
// these can override the Funcs inside no-layout templates also, use it when you know what you're doing
LayoutFuncs map[string]interface{}
}
// Jade the configs for JadeEngine
Jade HTMLTemplate
// Pongo the configs for PongoEngine
Pongo struct {
// Filters for pongo2, map[name of the filter] the filter function . The filters are auto register
Filters map[string]pongo2.FilterFunction
// Globals share context fields between templates. https://github.com/flosch/pongo2/issues/35
Globals map[string]interface{}
}
// Markdown the configs for MarkdownEngine
Markdown struct {
Sanitize bool // if true then returns safe html, default is false
}
// Amber the configs for AmberEngine
Amber struct {
// Funcs for the html/template result, amber default funcs are not overrided so use it without worries
Funcs template.FuncMap
}
)
// DefaultRest returns the default config for rest
func DefaultRest() Rest {
return Rest{
Charset: Charset,
IndentJSON: false,
IndentXML: false,
PrefixJSON: []byte(""),
PrefixXML: []byte(""),
UnEscapeHTML: false,
StreamingJSON: false,
DisableHTTPErrorRendering: false,
MarkdownSanitize: false,
}
}
// Merge merges the default with the given config and returns the result
func (c Rest) Merge(cfg []Rest) (config Rest) {
if len(cfg) > 0 {
config = cfg[0]
mergo.Merge(&config, c)
} else {
_default := c
config = _default
}
return
}
// MergeSingle merges the default with the given config and returns the result
func (c Rest) MergeSingle(cfg Rest) (config Rest) {
config = cfg
mergo.Merge(&config, c)
return
}
// DefaultTemplate returns the default template configs
func DefaultTemplate() Template {
return Template{
Engine: DefaultEngine, //or HTMLTemplate
Gzip: false,
IsDevelopment: false,
Directory: "templates",
Extensions: []string{".html"},
ContentType: "text/html",
Charset: "UTF-8",
Layout: "", // currently this is the only config which not working for pongo2 yet but I will find a way
HTMLTemplate: HTMLTemplate{Left: "{{", Right: "}}", Funcs: make(map[string]interface{}, 0), LayoutFuncs: make(map[string]interface{}, 0)},
Jade: Jade{Left: "{{", Right: "}}", Funcs: make(map[string]interface{}, 0), LayoutFuncs: make(map[string]interface{}, 0)},
Pongo: Pongo{Filters: make(map[string]pongo2.FilterFunction, 0), Globals: make(map[string]interface{}, 0)},
Markdown: Markdown{Sanitize: false},
Amber: Amber{Funcs: template.FuncMap{}},
}
}
// Merge merges the default with the given config and returns the result
func (c Template) Merge(cfg []Template) (config Template) {
if len(cfg) > 0 {
config = cfg[0]
mergo.Merge(&config, c)
} else {
_default := c
config = _default
}
return
}
// MergeSingle merges the default with the given config and returns the result
func (c Template) MergeSingle(cfg Template) (config Template) {
config = cfg
mergo.Merge(&config, c)
return
}