是不是真的不可能在树枝上做简单的数学或我错过了什么?如果我正在显示带有循环的项目,并且我想要总结项目价格我该怎么办?
{% for item in product %}
<tr>
<td> <img width="60" src="{{ asset('bundles/mpFrontend/assets/products/4.jpg') }}" alt=""/></td>
<td>{{ item.model }}</td>
<td>
<div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedInputButtons" size="16" type="text">
<button class="btn" type="button"><i class="icon-minus"></i></button>
<button class="btn" type="button"><i class="icon-plus"></i></button>
<button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white"></i></button>
</div>
</td>
<td>{{ item.price }}</td>
<td>{{ item.discount }}</td>
<td>{{ item.value }}</td>
<td>{{ item.pc }}</td>
</tr>
<tr>
<td colspan="6" align="right">Total Price: </td>
<td>{{ item.price|something }}</td> /// count here
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我的扩展课程:
<?php
// src/Mp/ShopBundle/Twig/AppExtension.php
namespace Mp\ShopBundle\Twig;
class AppExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
'getTotalPrice' => new \Twig_Function_Method($this, 'getTotalPrice'));
}
public function getTotalPrice(Items $items)
{
$total = 0;
foreach($items as $item){
$total += $item->getPrice();
}
return $total;
}
public function getName()
{
return 'app_extension';
}
}
Run Code Online (Sandbox Code Playgroud)
服务:
services:
app.twig_extension:
class: Mp\ShopBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
当我使用{{getTotalPrice(product)}}时,我在该行收到错误:
在渲染模板期间抛出异常("Catchable Fatal Error:传递给Mp\ShopBundle\Twig\AppExtension :: getTotalPrice()的参数1必须是Mp\ShopBundle\Twig\Items的实例,没有给出,调用在第177行的C:\ wamp\www\Digidis\tree\app\cache\dev\twig\b4\5d\b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php中,并在第94行的MpShopBundle:Frontend:product_summary.html.twig中定义了").
在 Twig 中求和的简短片段:
{% set total = 0 %}
{% for product in products %}
{% set total = total + product.getPrice() %}
{% endfor %}
Total: {{ total }}EUR
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1079 次 |
| 最近记录: |