如何在树枝页面中填充数组中的所有项目,而不是最新的

时间:2018-07-27 13:37:58

标签: php symfony twig

我是symfony的新手,我目前正在用symfony制作购物车。我需要知道的是,我目前正在传递使用数组购买的所有购物车商品。当我想显示details.html.twig页中数组内的所有项目时,它仅显示最新的项目。

var_dump中的传递数组

object(AppBundle\Entity\Cart)#188 (2) { 
    ["totalPrice":protected]=> int(1788) 
    ["cartBookItems":protected]=> array(2) { 
        [0]=> object(AppBundle\Entity\CartBookItem)#192 (3) { 
            ["bookID":protected]=> int(1) 
            ["quantity":protected]=> string(1) "1" 
            ["name":protected]=> string(12) "Harry Potter"
        } 
        [1]=> object(AppBundle\Entity\CartBookItem)#469 (3) { 
            ["bookID":protected]=> int(2) 
            ["quantity":protected]=> string(1) "2" 
            ["name":protected]=> string(9) "the Beast"
        }
    }
} 

仅显示最新的

enter image description here

详细信息页面中的代码

{% extends 'base.html.twig' %}
{% block body %}
    <ul class="nav navbar-nav navbar-right">
   <table class="table table-striped">
  <thead>
    <tr>
      <th scope="col">Total(LKR)</th>
      <th scope="col">Edit Cart</th>
    </tr>
   </thead>
   <tbody>
    <tr>
      <td>{{sessioncart.totalPrice}}</td>
      <td><li><a href="#"><span class="glyphicon glyphicon-shopping-cart"></span>Cart</a></li></td>
    </tr>
   </tbody>
   </table>
    </ul>   
    <h2 class="page-header">{{book.id}}</h2>
    <ul class="list-group">
    <li class="list-group-item">Category: {{book.bookname}}</li>
    <li class="list-group-item">Category: {{book.category}}</li>
    <li class="list-group-item">Price: {{book.price}}</li>
    <li class="list-group-item">Quantity: {{quantity}}</li>
    </ul>
    <a class="btn btn-default" href="/">Back to Index</a>
{% endblock %}

有人可以告诉我如何在表格中填充数组中的所有数据吗?

2 个答案:

答案 0 :(得分:1)

{% for book in cartBookItems %}
 here you could pass {{ book.bookID }} for example or {{ book.name }} for example
{% endfor %} 

当然,请确保您已添加“ cartBookItems”以从您的控制器进行渲染。

答案 1 :(得分:1)

在Twig中,有一条称为“ for”的指令,该指令用于遍历数组或类似对象。 代码如下所示:
{% for b in cartBookItems %} <li>Category: b.category</li> {% endfor %}