在树枝中使用数组变量

use an array variable in twig

我从控制器获得了一个数组形式的变量(变量广告),在树枝上,我使用了:

{% for ad in ads%}
<tr> <td> {{ad.xxx}}
 {% if app.user and app.user == ad.author%}
{{ad.yyyy}}
{% endif%}
</td> </tr>
{$ endfor%}

没问题,很经典。 我想使用例如

{% if app.user and app.user == ad.author%}

就在for循环之前,为登录用户隐藏一个应答器,但这不是他们的公告

如果 userAds 是同一作者的广告合集,您可以使用以下代码段:

{{ attribute(userAds|first, 'author') }}

{{ (userAds|first).author}}

{{ (userAds|first)['author'] }}

{% if app.user and (userAds|first).author == app.user %}
    <th>Action</th>
{% endif %}