26

Anyone knows how to addition two variable in twig I want to do something like:

{{ var1 }} + {{ var2 }}

2 Answers 2

46

Just do it inside the {{ }}

{{ var1 + var2 }}

If you want to assign it to some other variable:

{% set foo = var1 + var2 %}
{{ foo }}
0
15

Declaration :

{% set counter = 0 %}

Doing the addition :

{% set counter = counter + 1 %}

For displaying in twig template :

{{ counter }}
2
  • Helped! Thanks!! Apr 16, 2018 at 15:45
  • 2
    Worth clarifying the obvious that this example is best used in loops and the accepted answer for general addition with variables :)
    – DevLime
    Jun 22, 2018 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.