Twig Binding Operator

I want to calculate total numbers for a specific field in Twig

in php template, i can easily do it like this

  <?php $tl = 0; ?>
  <?php foreach($loo as $l):>
  <?php $tl += $l['amount'] ?>
  <tr>
    <td><?php echo $l['amount'] ?>
  </tr>
  <?php endforeach ?>

  <p><?php echo number_format($tl,2) ?>

How to do it in Twig?

I tried

 {% set tl = 0 %}
    {% for task in tasks %}
       {% set tl += {{ task.amount }} %} 
    {% endfor %}
    {{ tl }}

This does not work. Any ideas?

+4
source share
1 answer

It seems that the branch does not support combined operators, as PHP does. (I could not find an example at http://twig.sensiolabs.org/doc/templates.html#setting-variables )

Perhaps this is relevant: how to make the addition of 2 variable branches?

Can I use a separate version of the operator?

{% set tl = tl + task.amount %}
+4
source

Source: https://habr.com/ru/post/1625273/


All Articles