2015-04-24 22 views
6

Dalmada basit matematik yapmak gerçekten mümkün mü değil yoksa bir şey mi özlüyorum? Bir döngü içeren öğeler görüntülüyorsam ve ürün fiyatlarını toplamak istersem ne yapabilirim?Symfony2 dal şablonunda sayma

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

GÜNCELLEME

Benim uzatma sınıfı:

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

Hizmeti:

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

i kullanmak

{{getTotalPrice (ürün)}} Ben bir hata alıyorum bu satırda:

Bir şablonun oluşturulması sırasında bir istisna atıldı ("Catchable Fatal Error: Argüman 1 Mp \ ShopBundle \ Twig \ AppExtension :: getTotalPrice() öğesine iletildi) Mp \ ShopBundle \ Twig \ Items örneğinin olması gerekir. , satır 177'de C: \ wamp \ www \ Digidis \ tree \ app \ cache \ dev \ twig \ b4 \ 5d \ b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php ve MpShopBundle'da ") tanımlanan") Frontend: product_summary.html.twig satır 94'te çağrıldı. t bile

,

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

ben

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

Tek ihtiyacınız bu gibi basit bir yöntemdir –

+0

saymak için javascript veya jquery kullanmanızı öneririz Yani gerçekten twig ile basit matematik yapamazsınız? Sadece özetlemek için javascript yazmalısınız? Adam bir serseri. – Dominykas55

cevap

3

Kısa ön Twig bir miktar yapmak Twig'de hesaplama yapabileceğinizi düşünerek, ilk etapta onun rolü değil. Denetleyicinizden veya bir Servis'ten yapın veya bunun yerine bir Twig işlevini kullanın.

Ancak Twig Peki ya bu, sizin için gerekli olup olmadığını:

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

Her yineleme toplam yaklaşık bilemez ve kolayca sergileyelim.

0

Her zaman bunun için kendi yönteminizi yazabilirsiniz. belgelerine bir göz atın:

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

ve sonra Thsi gibi şablonda kullanmak:

{{ getTotalPrice(product) }} 
+0

Neden getName bölümünün neden gerekli olduğunu açıklayabilir misiniz? – Dominykas55

+0

Twig'in ne için kullandığını bilmiyorum ama bu yöntemi uygulamak zorunda olduğunuz 'Twig_ExtensionInterface' arabiriminin bir parçası olduğundan. Anlayabildiğim kadarıyla istisna mesajlarını biçimlendirmek için kullanılır. –

+0

Tamam, ben her şeyi örnek ile yaptım ve service.yml servisine belgelerde olduğu gibi ekledim, ancak getTotalPrice'nin bulunmadığı bir hatayı alıyorum? – Dominykas55