Skip to content

Commit 57adc53

Browse files
x3robep
authored andcommitted
tpl: Add float template function
Add a template function that allows conversion to float. This is useful, for example, when passing aspect ratios into templates, which tend to not be integers. Fixes #3307
1 parent b277cb3 commit 57adc53

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

‎docs/content/functions/float.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: float
3+
linktitle: float
4+
description: Creates a `float` from the argument passed into the function.
5+
godocref:
6+
date: 2017-09-28
7+
publishdate: 2017-09-28
8+
lastmod: 2017-09-28
9+
categories: [functions]
10+
menu:
11+
docs:
12+
parent: "functions"
13+
keywords: [strings,floats]
14+
signature: ["float INPUT"]
15+
workson: []
16+
hugoversion:
17+
relatedfuncs: []
18+
deprecated: false
19+
aliases: []
20+
---
21+
22+
Useful for turning strings into floating point numbers.
23+
24+
```
25+
{{ float "1.23" }} → 1.23
26+
```

‎tpl/cast/cast.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ type Namespace struct {
3030

3131
// ToInt converts the given value to an int.
3232
func (ns *Namespace) ToInt(v interface{}) (int, error) {
33+
v = convertTemplateToString(v)
34+
return _cast.ToIntE(v)
35+
}
36+
37+
// ToString converts the given value to a string.
38+
func (ns *Namespace) ToString(v interface{}) (string, error) {
39+
return _cast.ToStringE(v)
40+
}
41+
42+
// ToFloat converts the given value to a float.
43+
func (ns *Namespace) ToFloat(v interface{}) (float64, error) {
44+
v = convertTemplateToString(v)
45+
return _cast.ToFloat64E(v)
46+
}
47+
48+
func convertTemplateToString(v interface{}) interface{} {
3349
switch vv := v.(type) {
3450
case template.HTML:
3551
v = string(vv)
@@ -42,10 +58,5 @@ func (ns *Namespace) ToInt(v interface{}) (int, error) {
4258
case template.JSStr:
4359
v = string(vv)
4460
}
45-
return _cast.ToIntE(v)
46-
}
47-
48-
// ToString converts the given value to a string.
49-
func (ns *Namespace) ToString(v interface{}) (string, error) {
50-
return _cast.ToStringE(v)
61+
return v
5162
}

‎tpl/cast/cast_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,40 @@ func TestToString(t *testing.T) {
8181
assert.Equal(t, test.expect, result, errMsg)
8282
}
8383
}
84+
85+
func TestToFloat(t *testing.T) {
86+
t.Parallel()
87+
88+
ns := New()
89+
90+
for i, test := range []struct {
91+
v interface{}
92+
expect interface{}
93+
}{
94+
{"1", 1.0},
95+
{template.HTML("2"), 2.0},
96+
{template.CSS("3"), 3.0},
97+
{template.HTMLAttr("4"), 4.0},
98+
{template.JS("-5.67"), -5.67},
99+
{template.JSStr("6"), 6.0},
100+
{"1.23", 1.23},
101+
{"-1.23", -1.23},
102+
{"0", 0.0},
103+
{float64(2.12), 2.12},
104+
{int64(123), 123.0},
105+
{2, 2.0},
106+
{t, false},
107+
} {
108+
errMsg := fmt.Sprintf("[%d] %v", i, test.v)
109+
110+
result, err := ns.ToFloat(test.v)
111+
112+
if b, ok := test.expect.(bool); ok && !b {
113+
require.Error(t, err, errMsg)
114+
continue
115+
}
116+
117+
require.NoError(t, err, errMsg)
118+
assert.Equal(t, test.expect, result, errMsg)
119+
}
120+
}

‎tpl/cast/init.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func init() {
4343
},
4444
)
4545

46+
ns.AddMethodMapping(ctx.ToFloat,
47+
[]string{"float"},
48+
[][2]string{
49+
{`{{ "1234" | float | printf "%T" }}`, `float64`},
50+
},
51+
)
52+
4653
return ns
4754

4855
}

0 commit comments

Comments
 (0)