Symfony2 account in twig template

Is it really impossible to do simple math in a branch, or am I missing something? If I show items with a loop and I want to summarize the prices of goods, what can I do?

{% 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

My extension class:

 <?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'; } } 

Service:

 services: app.twig_extension: class: Mp\ShopBundle\Twig\AppExtension public: false tags: - { name: twig.extension } 

When I use {{getTotalPrice (product)}}, I get an error on this line:

An exception was thrown during template rendering ("Catchable Fatal Error: argument 1 passed to Mp \ ShopBundle \ Twig \ AppExtension :: getTotalPrice () must be an instance of Mp \ ShopBundle \ Twig \ Items, none specified, is called in C: \ wamp \ www \ Digidis \ tree \ app \ cache \ dev \ twig \ b4 \ 5d \ b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php on line 177 and defined by ") in MpShopBundle: Frontend: product_sumtm.

+6
source share
3 answers

A short snippet to make an amount in Twig:

 {% set total = 0 %} {% for product in products %} {% set total = total + product.getPrice() %} {% endfor %} Total: {{ total }}EUR 
+3
source

Dominykas55,

Even if you can do calculations in Twig, this is not his role in the first place. Do this from your controller or service or use the Twig function.

However, if Twig is required for you, then about this:

 {% set total = 0 %} {% for item in product %} {% set total = total + total.price %} {% endfor %} {{ total }} 

Each iteration will know about the results, and you can easily display it.

0
source

You can always write your own method. Take a look at the documentation:

http://symfony.com/doc/current/cookbook/templating/twig_extension.html

All you need is an easy way:

 class AppExtension extends \Twig_Extension { /** * Returns a list of functions to add to the existing list. * * @return array An array of functions */ 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'; } } 

and then use it in a template like thsi:

 {{ getTotalPrice(product) }} 
0
source

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


All Articles