I want to initialize multiple variables using a single statement in twig page. How I can do it? Below is my code and it looks horrible.
{% set t1Tot = 0 %}
{% set t2Tot = 0 %}
{% set t3Tot = 0 %}
{% set c1Tot = 0 %}
{% set c2Tot = 0 %}
{% set c3Tot = 0 %}
{% set o1Tot = 0 %}
{% set o2Tot = 0 %}
{% set o3Tot = 0 %}
{% set grandTot = 0 %}
When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.
You can assign multiple values to multiple variables by separating variables and values with commas , . You can assign to more than three variables. It is also possible to assign to different types. If there is one variable on the left side, it is assigned as a tuple.
raw. By default, everything in Twig gets escaped when automatic escaping is enabled. If you don't want to escape a variable you'll have to explicitly mark it as safe which you can do by using the raw filter. This only works if the raw filter is the last filter that is applied to the filter.
You can assign multiple variables in single statement as per docs
{% set foo, bar = 'foo', 'bar' %}
{% set t1Tot,t2Tot,t3Tot = 0,0,0 /* ... */ %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With