-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
136 lines (128 loc) · 4.15 KB
/
template.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
package gotemplate
import (
"bytes"
"regexp"
"strings"
"sync"
"text/template"
)
var flock = sync.Mutex{}
var fmap = template.FuncMap{
"add": add,
"concat": concat, // concat "a" "b" => "ab"
"contains": contains, // contains "a" "abc" => true
"createMap": createMap,
"date": dateFmt, // "2017-03-31 19:59:11" | date "06.01.02" => "17.03.31"
"dateFrom": dateFmtLayout,
"datetime": datetime,
"decimal": decimalFmt, // 3.1415 decimal 6,2 => 3.14
"div": divide,
"elseifthen": notconditional, // elseifthen "a" "b" => b, elseifthen "" "b" => ""
"empty": empty, // empty [] => "", ["bah"] => "bah"
"escape": escape,
"explode": explode,
"filter": filterPath,
"fixlen": fixlen,
"fixlenr": fixlenright,
"float": tofloat, // float "0123.234" => 123.234
"formatUKDate": formatUKDate,
"hasPrefix": hasPrefix, // hasPrefix "a" "ab" => true
"hasSuffix": hasSuffix, // hasSuffix "a" "ba" => true
"ifthen": conditional, // ifthen "a" "b" => a, ifthen "" "b" => b
"in_array": inArray,
"int": toint, // int "0123" => 123
"isset": isSet,
"item": item, // item "a:b" ":" 0 => a
"json_decode": jsonDecode,
"json_encode": jsonEncode,
"json_escape": jsonEscape,
"json": asJSON,
"last": last,
"limit": limit,
"lower": strings.ToLower,
"mapto": mapto, // mapto "a" "a:True|b:False" "|:" => True
"match": regexp.MatchString,
"regexpReplace": regReplaceAll, // regexpReplace "[^a-zA-Z0-9]" "!as.d?f12∂3" => "asdf123"
"md5": md5hash,
"mkSlice": mkSlice,
"mul": multiply,
"nanotimestamp": nanotimestamp,
"replace": replace,
"reReplaceAll": reReplaceAll,
"sanitise": sanitise,
"sanitize": sanitise,
"seq": seq,
"setItem": setItem,
"sql": sqlEscape,
"sub": subtract,
"timeformat": timeFormat,
"timeformatminus": timeFormatMinus,
"timestamp": timestamp,
"title": strings.Title,
"toAbs": toAbs,
"tojson": jsonDecode, // backward compatibility
"toLower": strings.ToLower,
"toUpper": strings.ToUpper,
"ukdate": ukdate,
"ukdatetime": ukdatetime,
"unique": unique,
"unixtimestamp": unixtimestamp,
"upper": strings.ToUpper,
"url_path": urlPath, // SEO, Slugify
"urldecode": urldecode,
"urlencode": urlencode,
"xml_array": xmlArray,
"xml_decode": xmlDecode,
"xml_encode": xmlEncode,
"xml": xmlEncode,
}
// RegisterFunc registers a new template func to the template parser
func RegisterFunc(key string, templatefunc interface{}) {
flock.Lock()
defer flock.Unlock()
fmap[key] = templatefunc
}
// GetFuncs will return all usable template funcs as string slice
func GetFuncs() []string {
flock.Lock()
defer flock.Unlock()
keys := make([]string, 0, len(fmap))
for k := range fmap {
keys = append(keys, k)
}
return keys
}
// MustTemplate parses string as Go template, using data as scope
func MustTemplate(str string, data interface{}) string {
ret, err := Template(str, data)
if err != nil {
panic(err)
}
return ret
}
// TemplateDelim parses string with custom delimiters as Go template, using data as scope
func TemplateDelim(str string, data interface{}, begin, end string) (string, error) {
tmpl, err := template.New("test").Funcs(fmap).Delims(begin, end).Parse(str)
if err == nil {
var doc bytes.Buffer
err = tmpl.Execute(&doc, data)
if err != nil {
return "", err
}
return strings.Replace(doc.String(), "<no value>", "", -1), nil
}
return "", err
}
// Template parses string as Go template, using data as scope
func Template(str string, data interface{}) (string, error) {
tmpl, err := template.New("test").Funcs(fmap).Parse(str)
if err == nil {
var doc bytes.Buffer
err = tmpl.Execute(&doc, data)
if err != nil {
return "", err
}
return strings.Replace(doc.String(), "<no value>", "", -1), nil
}
return "", err
}