【问题标题】:Symfony2 displaying dynamically values from array to twigSymfony2 动态显示从数组到树枝的值
【发布时间】:2015-07-05 18:40:00
【问题描述】:

我有一个函数可以计算每个产品的最终价格并将其保存在一个数组中。现在,当我想显示这些值时,我被困在如何动态显示所有这些值上。

这是函数:

public function getTotal($items)
{
    $total = array();
    foreach($items as $key=>$item){
        $total[$key] = $item->getPrice() - $item->getDiscount() + $item->getValue();
    }
    return $total;
}

getValue、getDiscount 等所有方法都在工作。

这就是我尝试显示的方式:

{{ getTotal(product)[key]}}

问题是,当我写 {{ getTotal(product)[0]}}{{ getTotal(product)[1]}} 等时,我得到了正确的值,但只有 1 个产品。我需要从所有这些中获取值。

如果我这样做 {{ getTotal(product)[key]}} 我得到一个奇怪的错误:

第 89 行的 MpShopBundle:Frontend:product_summary.html.twig 中不存在键为“0、1”的数组的键“12”

我不知道为什么密钥等于 12?也许我必须写一些不同的东西?

更新 谢谢你的回答,我什至没有想过用树枝循环,但我终于得到了一些价值。但是,我不知道为什么,但是 twig 循环为每个产品分配了两个值。

应该是这样的:

Product1 : 0(key):145(value)
Product2 : 1:415

但事实就是这样:

Product1 : 0:145 1:415
Product2 : 0:145 1:415

这是树枝:

 {% if product is defined %}

            {% for key, item in cart %}

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

                  {% if getTotal(product) is iterable %}
                    {% for key, sum in getTotal(product) %}

                     <td>{{ key }}:{{ sum }}</td>

                  {% endfor %}
                {% endif %}

                </tr>

 {% endfor %}

【问题讨论】:

  • 错误是说您的数组只有键“1”和“2”。
  • 是的,现在我只有 2 个产品,所以第一个产品是 key 0,第二个是 1
  • 所以您在代码中的其他位置将 key 设置为 12。我看不到,您需要向我们展示您的其余代码。

标签: php arrays symfony twig


【解决方案1】:

使用树枝for loop

{% for total in getTotal(product) %}
  {{ total }}
{% endfor %}

【讨论】:

  • 您在 for 循环中弄乱了相同的变量名称。变量名称(键、项)必须是唯一的。所以我们也很难猜到,你想展示什么。但目前看来,您应该将周期更改为:{% for cartKey, cartItem in cart %}{% for productKey, productItem in product %}
  • 然后你可以删除getTotal(product)的循环并使用{{ getTotal(product)[productKey] }}
【解决方案2】:

您可以使用 twig for 循环。

例子:

{% if getTotal(product) is iterable %}
    {% for key, sum in getTotal(product) %}
        {{ key }}:{{ sum }}
    {% else %}
        empty array
    {% endfor %}
{% endif %}

【讨论】:

  • 您不应在两个循环中使用相同的var names (key)
【解决方案3】:

您不应该使用循环,并将您的函数从外部移动到 product 类的方法。

而不是循环:

<td>{{ product.total }}</td>

新功能:

class Product {
...
    public function getTotal()
    {
        return $this->getPrice() - $this->getDiscount() + $this->getValue();
    }
}

【讨论】:

  • 这弄乱了我的所有功能。为什么我的代码不好?
  • 改变了我的答案,新的解决方案
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2013-10-03
  • 1970-01-01
  • 2016-05-22
  • 2015-12-10
  • 2012-09-16
相关资源
最近更新 更多