简体   繁体   中英

Symfony2 displaying dynamically values from array to twig

I have a function in which I count the final price of each product and I save it in an array. Now when I want do display the values I am stuck at how to display all of them dynamicaly.

This is the function:

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

All of the methods getValue, getDiscount and etc are working.

This is how I try o display:

{{ getTotal(product)[key]}}

The problem is that when I write for example {{ getTotal(product)[0]}} or {{ getTotal(product)[1]}} and etc i get the correct value, but only of 1 product. I need to get the values from all of them.

If i do {{ getTotal(product)[key]}} im getting a strange error:

Key "12" for array with keys "0, 1" does not exist in MpShopBundle:Frontend:product_summary.html.twig at line 89

I have no idea why the key is equal to 12? Maybe I have to write something different?

UPDATE Thank you for the answers, I didnt even think about looping with twig, but I am finally getting some values. However, I dont know why but the twig loop assigns both values for each product.

This is how it should be:

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

But this is how it is:

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

This is the twig:

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

Use twig for loop :

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

You can use the twig for loop.

Example:

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

You shouldn't use a loop, and move your function from an external to a method of the product class.

Instead of loop:

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

New function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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