Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 counting in twig template

Tags:

php

twig

symfony

Is it really not possible to do simple math in twig or am I missing something? If I am displaying items with a loop and I want to sum the items prices 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 am getting an error at that line:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Mp\ShopBundle\Twig\AppExtension::getTotalPrice() must be an instance of Mp\ShopBundle\Twig\Items, none given, called in C:\wamp\www\Digidis\tree\app\cache\dev\twig\b4\5d\b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php on line 177 and defined") in MpShopBundle:Frontend:product_summary.html.twig at line 94.

like image 954
Dominykas55 Avatar asked Apr 24 '15 05:04

Dominykas55


1 Answers

Short snippet to make a sum in Twig :

{% set total = 0 %}
{% for product in products %}
    {% set total = total + product.getPrice() %}
{% endfor %}
Total: {{ total }}EUR
like image 177
DarkBee Avatar answered Oct 16 '22 02:10

DarkBee