summaryrefslogtreecommitdiffhomepage
path: root/tpl/cast/cast.go
diff options
context:
space:
mode:
Diffstat (limited to 'tpl/cast/cast.go')
-rw-r--r--tpl/cast/cast.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/tpl/cast/cast.go b/tpl/cast/cast.go
index 378467178..999b31214 100644
--- a/tpl/cast/cast.go
+++ b/tpl/cast/cast.go
@@ -30,6 +30,22 @@ type Namespace struct {
// ToInt converts the given value to an int.
func (ns *Namespace) ToInt(v interface{}) (int, error) {
+ v = convertTemplateToString(v)
+ return _cast.ToIntE(v)
+}
+
+// ToString converts the given value to a string.
+func (ns *Namespace) ToString(v interface{}) (string, error) {
+ return _cast.ToStringE(v)
+}
+
+// ToFloat converts the given value to a float.
+func (ns *Namespace) ToFloat(v interface{}) (float64, error) {
+ v = convertTemplateToString(v)
+ return _cast.ToFloat64E(v)
+}
+
+func convertTemplateToString(v interface{}) interface{} {
switch vv := v.(type) {
case template.HTML:
v = string(vv)
@@ -42,10 +58,5 @@ func (ns *Namespace) ToInt(v interface{}) (int, error) {
case template.JSStr:
v = string(vv)
}
- return _cast.ToIntE(v)
-}
-
-// ToString converts the given value to a string.
-func (ns *Namespace) ToString(v interface{}) (string, error) {
- return _cast.ToStringE(v)
+ return v
}