gpt4 book ai didi

php - Twig 赋值运算符

转载 作者:可可西里 更新时间:2023-10-31 23:02:47 31 4
gpt4 key购买 nike

我想计算 Twig 中特定字段的总数

在 Php 模板中,我可以很容易地做到这一点

  <?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) ?>

如何在 Twig 中实现?

我试过了

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

没用有什么想法吗?

最佳答案

看起来 twig 不像 PHP 那样支持组合运算符。(我在 http://twig.sensiolabs.org/doc/templates.html#setting-variables 中找不到示例)

也许这是相关的: how make addition from 2 variable twig?

你能试试单独的运营商版本吗?

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

关于php - Twig 赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34917995/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com