0

我正在尝试获取 pagerfanta 结果的字段名。

树枝模板

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

4

1 回答 1

0

您可以将对象转换为数组并进行迭代。您必须创建自己的树枝过滤器才能将对象转换为数组。

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

{% endfor %}
于 2016-12-01T10:47:45.393 回答