我正在尝试获取页面结果的字段名。
小枝模板
{% for post in posts %}
{{ dump(post) }}
{% endfor %}转储结果为:
Users {#764 ▼
-iduser: 11
-username: "xzvdsfg"
-password: "SHHHHHH"
-lastlogin: DateTime {#691 ▶}
-roles: []
}我想要在不知道的情况下获得每个条目的名称,以便将其放在一个表中,如下所示
<table>
<thead>
<th>{{ fieldname }}</th>
</thead>
<tbody>
<td>{{ fieldname.value }}</td>
</tbody>
</table>并得到这个结果
<table>
<thead>
<th>username</th>
</thead>
<tbody>
<td>xzvdsfg</td>
</tbody>
</table>我是Twig模板的新手,谢谢!
发布于 2016-12-01 18:47:45
您可以将对象强制转换为数组,然后迭代。你必须创建你自己的twig filter来将你的对象转换成数组。
{% for key, value in post|cast_to_array %}
{% endfor %}https://stackoverflow.com/questions/40906777
复制相似问题