我只是在学习Symfony3和Twig。不知道我做错了什么,但是我有一个DB表产品,其中包含了行'name‘、'price’和'description'。我想简单地将DB表转储到HTML表中。我所做的如下:
花枝:
<table>
{% for product in products %}
<tr>
{% for key,value in product %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>如果我在第一个for-循环开始时转储产品,则得到以下结果:
Product {#330 ▼
-id: 1
-name: "Keyboard"
-price: "19.99"
-description: "Ergonomic and stylish!"
}但是,键和值都是空的。
发布于 2016-07-11 15:19:00
你应该用这样的方法:
<table>
<tr>
<th>Name</th> <th>Price</th> <th>Description</th>
</tr>
{% for product in products %}
<tr>
<td>{{ product.getName }}</td>
<td>{{ product.getPrice }}</td>
<td>{{ product.getDescription }}</td>
</tr>
{% endfor %}
</table>代码取决于您的getter。
https://stackoverflow.com/questions/38303647
复制相似问题