[英]Symfony3/Twig templating: database object to table
我正在學習Symfony3和Twig。 不確定我在做什么錯,但是我有一個帶有表行'name','price'和'description'的數據庫表產品 。 我只想將數據庫表轉儲為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!"
}
但是, 鍵和值都為空。
您應該使用這樣的東西:
<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>
代碼取決於您的獲取者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.