首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony3 3/Twig模板:数据库对象到表

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

Stack Overflow用户
提问于 2016-07-11 09:36:43
回答 1查看 209关注 0票数 0

我只是在学习Symfony3和Twig。不知道我做错了什么,但是我有一个DB表产品,其中包含了行'name‘、'price’和'description'。我想简单地将DB表转储到HTML表中。我所做的如下:

花枝:

代码语言:javascript
复制
<table>
    {% for product in products %}
        <tr>
            {% for key,value in product %}
                <td>{{ value }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</table>

如果我在第一个for-循环开始时转储产品,则得到以下结果:

代码语言:javascript
复制
Product {#330 ▼
  -id: 1
  -name: "Keyboard"
  -price: "19.99"
  -description: "Ergonomic and stylish!"
}

但是,都是空的。

EN

回答 1

Stack Overflow用户

发布于 2016-07-11 15:19:00

你应该用这样的方法:

代码语言:javascript
复制
<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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38303647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档