Twig和pagerfanta获取fieldname和fieldname.value

时间:2016-12-01 09:24:26

标签: doctrine twig symfony pagerfanta

我试图获取pagerfanta结果的字段名。

Twig模板

{% 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模板的新手 感谢!!!!

1 个答案:

答案 0 :(得分:0)

您可以将对象强制转换为数组并迭代。您必须创建自己的twig filter才能将对象转换为数组。

{% for key, value in post|cast_to_array %}

{% endfor %}
相关问题