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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分