-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars.go
42 lines (38 loc) · 946 Bytes
/
vars.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
package salix
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
var globalVars = map[string]any{
"len": tmplLen,
"json": tmplJSON,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"hasPrefix": strings.HasPrefix,
"trimPrefix": strings.TrimPrefix,
"hasSuffix": strings.HasSuffix,
"trimSuffix": strings.TrimSuffix,
"trimSpace": strings.TrimSpace,
"equalFold": strings.EqualFold,
"count": strings.Count,
"split": strings.Split,
"join": strings.Join,
"replace": strings.Replace,
"replaceAll": strings.ReplaceAll,
"sprintf": fmt.Sprintf,
}
func tmplLen(v any) (int, error) {
val := reflect.ValueOf(v)
switch val.Kind() {
case reflect.Array, reflect.Slice, reflect.String, reflect.Map:
return val.Len(), nil
default:
return 0, fmt.Errorf("cannot get length of %T", v)
}
}
func tmplJSON(v any) (HTML, error) {
data, err := json.Marshal(v)
return HTML(data), err
}