简体   繁体   English

Symfony2在树枝模板中计数

[英]Symfony2 counting in twig template

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 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: 当我使用{{getTotalPrice(product)}}时,我在该行收到错误:

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. 在渲染模板期间抛出异常(“Catchable Fatal Error:传递给Mp \\ ShopBundle \\ Twig \\ AppExtension :: getTotalPrice()的参数1必须是Mp \\ ShopBundle \\ Twig \\ Items的实例,没有给出,调用在第177行的C:\\ wamp \\ www \\ Digidis \\ tree \\ app \\ cache \\ dev \\ twig \\ b4 \\ 5d \\ b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php中,并在第94行的MpShopBundle:Frontend:product_summary.html.twig中定义了“)。

Short snippet to make a sum in Twig : 在Twig中作出总结的简短片段:

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

Dominykas55, Dominykas55,

Even though you can do computation in Twig, it is not its role in first place. 即使你可以在Twig中进行计算,但它首先不是它的作用。 Do it from your Controller or a Service, or use a Twig function instead. 从您的控制器或服务中执行,或使用Twig功能。

However, if Twig is required for you, what about this: 但是,如果你需要Twig,那么:

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

Each iteration would know about the total, and could you easily display it. 每次迭代都会知道总数,并且您可以轻松地显示它。

You can always write you own method for it. 你总是可以为它编写自己的方法。 Take a look at the documentation: 看看文档:

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

All you need is a simple method like this: 你需要的只是一个简单的方法:

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 the template like thsi: 然后在模板中使用它,如thsi:

{{ getTotalPrice(product) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM