46 lines
948 B
Go
Raw Normal View History

2016-11-06 18:32:32 +01:00
package funcmap
import (
"encoding/json"
2016-12-01 10:08:51 +01:00
"fmt"
"strconv"
2016-11-06 18:32:32 +01:00
"strings"
"text/template"
)
var FuncMap = template.FuncMap{
"json": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
"prettyjson": func(v interface{}) string {
a, _ := json.MarshalIndent(v, "", " ")
return string(a)
},
// yaml
// xml
// toml
"split": strings.Split,
"join": strings.Join,
"title": strings.Title,
2016-12-01 10:08:51 +01:00
"unexport": func(input string) string {
return fmt.Sprintf("%s%s", strings.ToLower(input[0:1]), input[1:])
},
2016-11-06 18:32:32 +01:00
"lower": strings.ToLower,
"upper": strings.ToUpper,
2016-12-01 10:08:51 +01:00
"rev": func(v interface{}) string {
runes := []rune(v.(string))
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
},
"int": func(v interface{}) string {
a, err := strconv.Atoi(v.(string))
if err != nil {
return fmt.Sprintf("%v", v)
}
return fmt.Sprintf("%d", a)
},
2016-11-06 18:32:32 +01:00
}