Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony assign multiple variable in a single statement

Tags:

twig

symfony

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 %}
like image 413
user8689892 Avatar asked Oct 23 '17 07:10

user8689892


People also ask

How do I assign multiple variables to one line?

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.

Can you assign multiple variables at once?

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.

What is raw in twig?

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.


1 Answers

You can assign multiple variables in single statement as per docs

{% set foo, bar = 'foo', 'bar' %}

{% set t1Tot,t2Tot,t3Tot   = 0,0,0 /* ... */ %}
like image 69
M Khalid Junaid Avatar answered Sep 22 '22 22:09

M Khalid Junaid