You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29
Original file line number
Diff line number
Diff line change
@@ -165,6 +165,35 @@ Adds tests known from PHP, so you can test a value for being:
165
165
{% endif %}
166
166
```
167
167
168
+
### Convert to type: intval, strval, floatval and boolval filters
169
+
170
+
Converting a variable to a specific type is not something Twig encourages and it probably should be avoided, if possible. Yet there are situations where you just want to convert something to an integer or string so you can be sure a comparison is type safe or that there is no unexpected behavior because one value has the wrong type.
171
+
172
+
```twig
173
+
{% if '5'|intval === 5 %}
174
+
Convert '5' to an integer - this if block is being executed
175
+
{% endif %}
176
+
177
+
{% if 5.7|strval === '5.7' %}
178
+
Convert 5.7 to a string - this if block is being executed
179
+
{% endif %}
180
+
181
+
{% if 1|boolval === true %}
182
+
Convert 1 to a boolean - this if block is being executed
183
+
{% endif %}
184
+
185
+
{% if '5.7'|floatval === 5.7 %}
186
+
Convert '5.7' to a float - this if block is being executed
187
+
{% endif %}
188
+
```
189
+
190
+
These filters mainly behave like the ones in PHP (and use the corresponding PHP functions internally), but there is some additional behavior to detect or avoid likely errors:
191
+
192
+
- only scalar values, null and objects with a __toString method are allowed, so if you use any of these filters with an array or an object that cannot be cast to a string it will throw an exception
193
+
- null will return 0 for intval, '' for strval, false for boolval and 0.0 for floatval (just like in PHP)
194
+
- objects with a __toString method will be converted to a string first (using the __toString method), and only after that intval, boolval and floatval will be used
195
+
- boolval should be used with caution, as if you give it any non-numeric string it will return true, yet empty strings and "0" will return false. boolval is here more for completeness, as it is probably the least useful conversion function in PHP. The recommendation is to use the other three functions instead of using boolval if possible.
196
+
168
197
### && and ||
169
198
170
199
If you want to make expressions even more like PHP, you can use `&&` instead of `and` and `||` instead of `or`. This might be the least useful part of this library, as `and` and `or` are already short and clear, yet it is another easily remedied difference between Twig and PHP, and `&&` and `||` can be easier to spot in comparison to `and` and `or`.
"strtotime" filter exception test with non-scalar value
3
+
--TEMPLATE--
4
+
{% if obj|strval === '' %}
5
+
3
6
+
{% endif %}
7
+
--DATA--
8
+
return [
9
+
'obj' => new \stdClass(),
10
+
];
11
+
--EXCEPTION--
12
+
Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("Non-scalar value given to intval/floatval/strval/boolval filter") in "index.twig" at line 2.
0 commit comments