-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_functions.go
237 lines (205 loc) · 6.09 KB
/
template_functions.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 partial
import (
"fmt"
"html/template"
"net/url"
"strings"
"time"
"unicode"
)
var DefaultTemplateFuncMap = template.FuncMap{
"safeHTML": safeHTML,
// String functions
"upper": strings.ToUpper,
"lower": strings.ToLower,
"trimSpace": strings.TrimSpace,
"trim": strings.Trim,
"trimSuffix": strings.TrimSuffix,
"trimPrefix": strings.TrimPrefix,
"contains": strings.Contains,
"containsAny": strings.ContainsAny,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"repeat": strings.Repeat,
"replace": strings.Replace,
"split": strings.Split,
"join": strings.Join,
"stringSlice": stringSlice,
"title": title,
"substr": substr,
"ucfirst": ucfirst,
"compare": strings.Compare,
"equalFold": strings.EqualFold,
"urlencode": url.QueryEscape,
"urldecode": url.QueryUnescape,
// Time functions
"now": time.Now,
"formatDate": formatDate,
"parseDate": parseDate,
// List functions
"first": first,
"last": last,
// Map functions
"hasKey": hasKey,
"keys": keys,
// Debug functions
"debug": debug,
}
func safeHTML(s string) template.HTML {
return template.HTML(s)
}
// ucfirst capitalizes the first character of the string.
func ucfirst(s string) string {
if s == "" {
return ""
}
runes := []rune(s)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
func stringSlice(values ...string) []string {
return values
}
// title capitalizes the first character of each word in the string.
func title(s string) string {
if s == "" {
return ""
}
runes := []rune(s)
length := len(runes)
capitalizeNext := true
for i := 0; i < length; i++ {
if unicode.IsSpace(runes[i]) {
capitalizeNext = true
} else if capitalizeNext {
runes[i] = unicode.ToUpper(runes[i])
capitalizeNext = false
} else {
runes[i] = unicode.ToLower(runes[i])
}
}
return string(runes)
}
// substr returns a substring starting at 'start' position with 'length' characters.
func substr(s string, start, length int) string {
runes := []rune(s)
if start >= len(runes) || length <= 0 {
return ""
}
end := start + length
if end > len(runes) {
end = len(runes)
}
return string(runes[start:end])
}
// first returns the first element of the list.
func first(a []any) any {
if len(a) > 0 {
return a[0]
}
return nil
}
// last returns the last element of the list.
func last(a []any) any {
if len(a) > 0 {
return a[len(a)-1]
}
return nil
}
// hasKey checks if the map has the key.
func hasKey(m map[string]any, key string) bool {
_, ok := m[key]
return ok
}
// keys returns the keys of the map.
func keys(m map[string]any) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
return out
}
// formatDate formats the time with the layout.
func formatDate(t time.Time, layout string) string {
return t.Format(layout)
}
// parseDate parses the time with the layout.
func parseDate(layout, value string) (time.Time, error) {
return time.Parse(layout, value)
}
// debug returns the string representation of the value.
func debug(v any) string {
return fmt.Sprintf("%+v", v)
}
func selectionFunc(p *Partial, data *Data) func() template.HTML {
return func() template.HTML {
var selectedPartial *Partial
partials := p.getSelectionPartials()
if partials == nil {
p.getLogger().Error("no selection partials found", "id", p.id)
return template.HTML(fmt.Sprintf("no selection partials found in parent '%s'", p.id))
}
requestedSelect := p.getConnector().GetSelectValue(p.GetRequest())
if requestedSelect != "" {
selectedPartial = partials[requestedSelect]
} else {
selectedPartial = partials[p.selection.Default]
}
if selectedPartial == nil {
p.getLogger().Error("selected partial not found", "id", requestedSelect, "parent", p.id)
return template.HTML(fmt.Sprintf("selected partial '%s' not found in parent '%s'", requestedSelect, p.id))
}
selectedPartial.fs = p.fs
html, err := selectedPartial.renderSelf(data.Ctx, p.GetRequest())
if err != nil {
p.getLogger().Error("error rendering selected partial", "id", requestedSelect, "parent", p.id, "error", err)
return template.HTML(fmt.Sprintf("error rendering selected partial '%s'", requestedSelect))
}
return html
}
}
func childFunc(p *Partial, data *Data) func(id string, vals ...any) template.HTML {
return func(id string, vals ...any) template.HTML {
if len(vals) > 0 && len(vals)%2 != 0 {
p.getLogger().Warn("invalid child data for partial, they come in key-value pairs", "id", id)
return template.HTML(fmt.Sprintf("invalid child data for partial '%s'", id))
}
d := make(map[string]any)
for i := 0; i < len(vals); i += 2 {
key, ok := vals[i].(string)
if !ok {
p.getLogger().Warn("invalid child data key for partial, it must be a string", "id", id, "key", vals[i])
return template.HTML(fmt.Sprintf("invalid child data key for partial '%s', want string, got %T", id, vals[i]))
}
d[key] = vals[i+1]
}
html, err := p.renderChildPartial(data.Ctx, id, d)
if err != nil {
p.getLogger().Error("error rendering partial", "id", id, "error", err)
// Handle error: you can log it and return an empty string or an error message
return template.HTML(fmt.Sprintf("error rendering partial '%s': %v", id, err))
}
return html
}
}
func actionFunc(p *Partial, data *Data) func() template.HTML {
return func() template.HTML {
if p.templateAction == nil {
p.getLogger().Error("no action callback found", "id", p.id)
return template.HTML(fmt.Sprintf("no action callback found in partial '%s'", p.id))
}
// Use the selector to get the appropriate partial
actionPartial, err := p.templateAction(data.Ctx, p, data)
if err != nil {
p.getLogger().Error("error in selector function", "error", err)
return template.HTML(fmt.Sprintf("error in action function: %v", err))
}
// Render the selected partial instead
html, err := actionPartial.renderSelf(data.Ctx, p.GetRequest())
if err != nil {
p.getLogger().Error("error rendering action partial", "error", err)
return template.HTML(fmt.Sprintf("error rendering action partial: %v", err))
}
return html
}
}