2015-04-24 7 views
6

यह वास्तव में संभव नहीं टहनी में सरल गणित करने के लिए है या मैं कुछ याद आ रही है? यदि मैं लूप के साथ आइटम प्रदर्शित कर रहा हूं और मैं आइटम की कीमतों को जोड़ना चाहता हूं तो मैं क्या कर सकता हूं?Symfony2 टहनी टेम्पलेट में गिनती

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

अद्यतन

मेरे विस्तार वर्ग:

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

सेवा:

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

जब मैं का उपयोग {{getTotalPrice (उत्पाद)}} मैं एक त्रुटि हो रही है उस रेखा पर:

एक अपवाद एक टेम्पलेट ("Catchable घातक त्रुटि का प्रतिपादन के दौरान दिया गया है: तर्क 1 Mp \ ShopBundle \ टहनी \ AppExtension :: getTotalPrice (के लिए पारित) Mp \ ShopBundle \ टहनी \ आइटम का एक उदाहरण होना चाहिए, कोई नहीं दिया , जिसे सी: \ wamp \ www \ Digidis \ tree \ app \ cache \ dev \ twig \ b4 \ 5d \ b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php पंक्ति 177 पर परिभाषित किया गया है और परिभाषित किया गया है) mpShopBundle में: Frontend: product_summary.html.twig लाइन 94 पर।

+0

मैं

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

आपको बस इस तरह एक सरल तरीका है आप –

+0

गिनती करने के लिए तो तुम सच में नहीं कर सकते जावास्क्रिप्ट या jQuery का उपयोग करने का सुझाव टहनी के साथ सरल गणित करते हैं? बस योग करने के लिए आपको जावास्क्रिप्ट लिखना है? आदमी एक बमर है। – Dominykas55

उत्तर

3

लघु स्निपेट टहनी में राशि बनाने के लिए:

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

Dominykas55,

यहां तक ​​कि टी आटा आप टवीग में गणना कर सकते हैं, यह पहली जगह में इसकी भूमिका नहीं है। इसे अपने कंट्रोलर या सेवा से करें, या इसके बजाय एक ट्विग फ़ंक्शन का उपयोग करें।

हालांकि, टहनी आप के लिए आवश्यक है, इसके बारे में क्या करता है, तो:

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

प्रत्येक पुनरावृत्ति कुल के बारे में पता होता है, और आप आसानी से इसे प्रदर्शित कर सकता है।

0

तुम हमेशा आप ही इसके लिए विधि लिख सकते हैं। प्रलेखन पर एक नज़र डालें:

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

और फिर thsi तरह टेम्पलेट में इसका इस्तेमाल करते हैं:

{{ getTotalPrice(product) }} 
+0

क्या आप मुझे समझा सकते हैं कि getName भाग की आवश्यकता क्यों है? – Dominykas55

+0

मुझे यकीन है कि क्या टहनी के लिए, लेकिन यह 'Twig_ExtensionInterface' इंटरफेस आप इस विधि को लागू करने के लिए है का हिस्सा है के बाद से यह उपयोग कर रहा है नहीं कर रहा हूँ। जहां तक ​​मैं इसे बता सकता हूं इसका उपयोग अपवाद संदेशों और प्रोफाइलरों में प्रारूपित करने के लिए किया जाता है। –

+0

ठीक है, मैं अपने उदाहरण के साथ सब कुछ किया था, और दस्तावेज में की तरह services.yml में सेवा जोड़ा, फिर भी एक त्रुटि है कि getTotalPrice मौजूद नहीं है हो रही im? – Dominykas55

संबंधित मुद्दे