forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_adapter_go.go
289 lines (268 loc) · 8.66 KB
/
template_adapter_go.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package revel
import (
"bytes"
"fmt"
"html"
"html/template"
"io"
"log"
"reflect"
"strings"
"time"
)
const GO_TEMPLATE = "go"
var (
// The functions available for use in the templates.
TemplateFuncs = map[string]interface{}{
"url": ReverseURL,
"set": func(viewArgs map[string]interface{}, key string, value interface{}) template.JS {
viewArgs[key] = value
return template.JS("")
},
"append": func(viewArgs map[string]interface{}, key string, value interface{}) template.JS {
if viewArgs[key] == nil {
viewArgs[key] = []interface{}{value}
} else {
viewArgs[key] = append(viewArgs[key].([]interface{}), value)
}
return template.JS("")
},
"field": NewField,
"firstof": func(args ...interface{}) interface{} {
for _, val := range args {
switch val.(type) {
case nil:
continue
case string:
if val == "" {
continue
}
return val
default:
return val
}
}
return nil
},
"option": func(f *Field, val interface{}, label string) template.HTML {
selected := ""
if f.Flash() == val || (f.Flash() == "" && f.Value() == val) {
selected = " selected"
}
return template.HTML(fmt.Sprintf(`<option value="%s"%s>%s</option>`,
html.EscapeString(fmt.Sprintf("%v", val)), selected, html.EscapeString(label)))
},
"radio": func(f *Field, val string) template.HTML {
checked := ""
if f.Flash() == val {
checked = " checked"
}
return template.HTML(fmt.Sprintf(`<input type="radio" name="%s" value="%s"%s>`,
html.EscapeString(f.Name), html.EscapeString(val), checked))
},
"checkbox": func(f *Field, val string) template.HTML {
checked := ""
if f.Flash() == val {
checked = " checked"
}
return template.HTML(fmt.Sprintf(`<input type="checkbox" name="%s" value="%s"%s>`,
html.EscapeString(f.Name), html.EscapeString(val), checked))
},
// Pads the given string with 's up to the given width.
"pad": func(str string, width int) template.HTML {
if len(str) >= width {
return template.HTML(html.EscapeString(str))
}
return template.HTML(html.EscapeString(str) + strings.Repeat(" ", width-len(str)))
},
"errorClass": func(name string, viewArgs map[string]interface{}) template.HTML {
errorMap, ok := viewArgs["errors"].(map[string]*ValidationError)
if !ok || errorMap == nil {
WARN.Println("Called 'errorClass' without 'errors' in the view args.")
return template.HTML("")
}
valError, ok := errorMap[name]
if !ok || valError == nil {
return template.HTML("")
}
return template.HTML(ErrorCSSClass)
},
"msg": func(viewArgs map[string]interface{}, message string, args ...interface{}) template.HTML {
str, ok := viewArgs[CurrentLocaleViewArg].(string)
if !ok {
return ""
}
return template.HTML(MessageFunc(str, message, args...))
},
// Replaces newlines with <br>
"nl2br": func(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
},
// Skips sanitation on the parameter. Do not use with dynamic data.
"raw": func(text string) template.HTML {
return template.HTML(text)
},
// Pluralize, a helper for pluralizing words to correspond to data of dynamic length.
// items - a slice of items, or an integer indicating how many items there are.
// pluralOverrides - optional arguments specifying the output in the
// singular and plural cases. by default "" and "s"
"pluralize": func(items interface{}, pluralOverrides ...string) string {
singular, plural := "", "s"
if len(pluralOverrides) >= 1 {
singular = pluralOverrides[0]
if len(pluralOverrides) == 2 {
plural = pluralOverrides[1]
}
}
switch v := reflect.ValueOf(items); v.Kind() {
case reflect.Int:
if items.(int) != 1 {
return plural
}
case reflect.Slice:
if v.Len() != 1 {
return plural
}
default:
ERROR.Println("pluralize: unexpected type: ", v)
}
return singular
},
// Format a date according to the application's default date(time) format.
"date": func(date time.Time) string {
return date.Format(DateFormat)
},
"datetime": func(date time.Time) string {
return date.Format(DateTimeFormat)
},
"slug": Slug,
"even": func(a int) bool { return (a % 2) == 0 },
"i18ntemplate": func(args ...interface{}) (template.HTML, error) {
templateName, lang := "", ""
var viewArgs interface{}
switch len(args) {
case 0:
ERROR.Printf("No arguements passed to template call")
case 1:
// Assume only the template name is passed in
templateName = args[0].(string)
case 2:
// Assume template name and viewArgs is passed in
templateName = args[0].(string)
viewArgs = args[1]
// Try to extract language from the view args
if viewargsmap, ok := viewArgs.(map[string]interface{}); ok {
lang, _ = viewargsmap[CurrentLocaleViewArg].(string)
}
default:
// Assume third argument is the region
templateName = args[0].(string)
viewArgs = args[1]
lang, _ = args[2].(string)
if len(args) > 3 {
ERROR.Printf("Received more parameters then needed for %s", templateName)
}
}
var buf bytes.Buffer
// Get template
tmpl, err := MainTemplateLoader.TemplateLang(templateName, lang)
if err == nil {
err = tmpl.Render(&buf, viewArgs)
} else {
ERROR.Printf("Failed to render i18ntemplate %s %v", templateName, err)
}
return template.HTML(buf.String()), err
},
}
)
// Adapter for Go Templates.
type GoTemplate struct {
*template.Template
engine *GoEngine
*TemplateView
}
// return a 'revel.Template' from Go's template.
func (gotmpl GoTemplate) Render(wr io.Writer, arg interface{}) error {
return gotmpl.Execute(wr, arg)
}
type GoEngine struct {
loader *TemplateLoader
templateSet *template.Template
// TemplatesBylowerName is a map from lower case template name to the real template.
templatesBylowerName map[string]*GoTemplate
splitDelims []string
CaseInsensitiveMode bool
}
func (i *GoEngine) ConvertPath(path string) string {
if i.CaseInsensitiveMode {
return strings.ToLower(path)
}
return path
}
func (i *GoEngine) Handles(templateView *TemplateView) bool {
return EngineHandles(i, templateView)
}
func (engine *GoEngine) ParseAndAdd(baseTemplate *TemplateView) error {
// If alternate delimiters set for the project, change them for this set
is_path_prefix, _ := hasPathPrefix(baseTemplate.Location(), ViewsPath, false)
if engine.splitDelims != nil && is_path_prefix {
engine.templateSet.Delims(engine.splitDelims[0], engine.splitDelims[1])
} else {
// Reset to default otherwise
engine.templateSet.Delims("", "")
}
templateSource := string(baseTemplate.FileBytes)
lowerTemplateName := engine.ConvertPath(baseTemplate.TemplateName)
tpl, err := engine.templateSet.New(baseTemplate.TemplateName).Parse(templateSource)
if nil != err {
_, line, description := ParseTemplateError(err)
return &Error{
Title: "Template Compilation Error",
Path: baseTemplate.TemplateName,
Description: description,
Line: line,
SourceLines: strings.Split(templateSource, "\n"),
}
}
engine.templatesBylowerName[lowerTemplateName] = &GoTemplate{Template: tpl, engine: engine, TemplateView: baseTemplate}
return nil
}
func (engine *GoEngine) Lookup(templateName string) Template {
// Case-insensitive matching of template file name
if tpl, found := engine.templatesBylowerName[engine.ConvertPath(templateName)]; found {
return tpl
}
return nil
}
func (engine *GoEngine) Name() string {
return GO_TEMPLATE
}
func (engine *GoEngine) Event(action int, i interface{}) {
if action == TEMPLATE_REFRESH_REQUESTED {
// At this point all the templates have been passed into the
engine.templatesBylowerName = map[string]*GoTemplate{}
engine.templateSet = template.New("__root__").Funcs(TemplateFuncs)
// Check to see what should be used for case sensitivity
engine.CaseInsensitiveMode = Config.StringDefault("go.template.path", "lower") != "case"
}
}
func init() {
RegisterTemplateLoader(GO_TEMPLATE, func(loader *TemplateLoader) (TemplateEngine, error) {
// Set the template delimiters for the project if present, then split into left
// and right delimiters around a space character
TemplateDelims := Config.StringDefault("template.go.delimiters", "")
var splitDelims []string
if TemplateDelims != "" {
splitDelims = strings.Split(TemplateDelims, " ")
if len(splitDelims) != 2 {
log.Fatalln("app.conf: Incorrect format for template.delimiters")
}
}
return &GoEngine{
loader: loader,
templateSet: template.New("__root__").Funcs(TemplateFuncs),
templatesBylowerName: map[string]*GoTemplate{},
splitDelims: splitDelims,
}, nil
})
}