2016-01-12 22 views
0

當用戶未登錄時app.user爲空 - 我知道這一點。Symfony2 anon。當app.user爲null時,app中的app.user的訪問屬性

我在我的樹枝模板中有一個用於檢查通知的部分。

{% if app.user.notificationsNews == 1 %} 
<span class="challenge-notify animated rotateIn">!</span> 
{% endif %} 

沒有用戶,沒有通知,沒有問題。除了良好的用戶爲空,它仍然想訪問屬性

不可能在一個空 變量

訪問屬性(「notificationsNews」)是有繞過一個好辦法嗎? |默認沒有竅門

我可以檢查,在我的控制器,並返回一個不同的模板文件,我只是想知道是否有辦法做到這一點在樹枝。

回答

1

你要善於用這

{% if app.user and app.user.notificationsNews == 1 %} 
    <span class="challenge-notify animated rotateIn">!</span> 
{% endif %} 
0

去,如果你是和我一樣懶,你可以使用|default過濾器:

{% if app.user.notificationsNews|default(0) == 1 %} 
<span class="challenge-notify animated rotateIn">!</span> 
{% endif %} 

fiddle

相關問題