Rewrite first argument to interface{}
authorTom Helmer Hansen <tom@adapt.dk>
Sat, 17 Jan 2015 14:46:27 +0000 (15:46 +0100)
committerbep <bjorn.erik.pedersen@gmail.com>
Sun, 18 Jan 2015 13:54:26 +0000 (14:54 +0100)
tpl/template.go

index f392f81f0d2a877e2a4dd90d016fc6d3eeeb1a54..1ddc04b57bcfa2e3cc7ac7eaaf8aaa0ebb984bc1 100644 (file)
@@ -880,6 +880,24 @@ func Chomp(text interface{}) (string, error) {
        return chompRegexp.ReplaceAllString(s, ""), nil
 }
 
+// Trim leading/trailing characters defined by b from a
+func Trim(a interface{}, b string) (string, error) {
+       aStr, err := cast.ToStringE(a)
+       if err != nil {
+               return "", err
+       }
+       return strings.Trim(aStr, b), nil
+}
+
+// Replace all occurences of b with c in a
+func Replace(a interface{}, b string, c string) (string, error) {
+       aStr, err := cast.ToStringE(a)
+       if err != nil {
+               return "", err
+       }
+       return strings.Replace(aStr, b, c, -1), nil
+}
+
 func SafeHtml(text string) template.HTML {
        return template.HTML(text)
 }
@@ -1237,8 +1255,8 @@ func init() {
                "relref":      RelRef,
                "apply":       Apply,
                "chomp":       Chomp,
-    "replace":     func(a string, b string, c string) string { return strings.Replace(a, b, c, -1) },
-    "trim":        func(a string, b string) string { return strings.Trim(a, b) },
+               "replace":     Replace,
+               "trim":        Trim,
        }
 
        chompRegexp = regexp.MustCompile("[\r\n]+$")