Czy naprawdę nie można wykonać prostej matematyki w gałązce czy czegoś brakuje? Jeśli wyświetlam elementy z pętlą i chcę podsumować ceny przedmiotów, co mogę zrobić?Symfony2 liczenie w szablonie gałązki
{% 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 %}
UPDATE
Moja klasa rozszerzenie:
<?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';
}
}
Usługa:
services:
app.twig_extension:
class: Mp\ShopBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
Kiedy używać {{getTotalPrice (produkt)}} Otrzymuję błąd w tej linii:
Wyjątek został zgłoszony podczas renderowania szablonu ("Złapalny błąd krytyczny: Argument 1 przekazany do Mp \ ShopBundle \ Twig \ AppExtension :: getTotalPrice() musi być instancją Mp \ ShopBundle \ Twig \ Items, none given , wywołane w C: \ wamp \ www \ Digidis \ tree \ app \ cache \ dev \ twig \ b4 \ 5d \ b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php na linii 177 i zdefiniowane ") w MpShopBundle: Frontend: product_summary.html.twig w linii 94.
I sugeruję użycie javascript lub jquery do zliczenia –
Więc naprawdę nie możesz zrobić prostej matematyki z gałązką? Żeby podsumować, musisz napisać javascript? Człowieku, to jest bummer. – Dominykas55