I'm just learning Symfony3 and Twig. Unsure what I'm doing wrong, but I have a DB table product with rows 'name', 'price' and 'description'. I want to simply dump the DB table into an HTML table. I did as follows:
Twig:
<table>
{% for product in products %}
<tr>
{% for key,value in product %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
if I dump product at the beginning of the first for-loop, I get the following result:
Product {#330 ▼
-id: 1
-name: "Keyboard"
-price: "19.99"
-description: "Ergonomic and stylish!"
}
however, key and value are both empty.
You should use something like this:
<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>
The code depends on your getters.