douhuo3696 2016-07-11 09:36
浏览 47

Symfony3 / Twig模板:数据库对象到表

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.

  • 写回答

1条回答 默认 最新

  • duanletao9487 2016-07-11 15:19
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题