Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how make addition from 2 variable twig?

Tags:

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

{{ var1 }} + {{ var2 }}

like image 365
Mark Cibor Avatar asked Feb 13 '12 23:02

Mark Cibor


People also ask

How do I add a global variable in Twig?

If you are using Twig in another project, you can set your globals directly in the environment: $twig = new Twig_Environment($loader); $twig->addGlobal('myStuff', $someVariable); And then use {{ myStuff }} anywhere in your application.


2 Answers

Just do it inside the {{ }}

{{ var1 + var2 }} 

If you want to assign it to some other variable:

{% set foo = var1 + var2 %} {{ foo }} 
like image 70
solarc Avatar answered Oct 07 '22 05:10

solarc


Declaration :

{% set counter = 0 %} 

Doing the addition :

{% set counter = counter + 1 %} 

For displaying in twig template :

{{ counter }} 
like image 42
Rodolfo Velasco Avatar answered Oct 07 '22 04:10

Rodolfo Velasco