可用时显示用户图片

Display user picture when available

我正在尝试显示用户帐户图片,如果图片字段不在基础上 'null'。

如果字段是 'null' 我会显示默认的帐户图片。

我试过 {{ if MYFIELD is null }}{{ if MYFIELD is define }}

像这样

<img src="{% if user.pictureName is null %}{{ asset('bundles/nastycodefront/img/userpic.png') }}{% else %}{{ asset('uploads/pictures/nastypic_' ~ post.member ~ '.jpg') }}{% endif %}" alt="Profile Picture">

<img src="{% if user.pictureName is define %}{{ asset('uploads/pictures/nastypic_' ~ post.member ~ '.jpg') }}{% else %}{{ asset('bundles/nastycodefront/img/userpic.png') }}{% endif %}" alt="Profile Picture">

但所有这些都在不断尝试获取自定义图像。

你有办法解决这个问题吗?我的代码有错吗?

感谢您的帮助

试试这个

<img src="{% if app.user.pictureName is defined and app.user.pictureName|length > 0 %}{{ asset('uploads/pictures/nastypic_' ~ app.user.pictureName ~ '.jpg') }}{% else %}{{asset('bundles/nastycodefront/img/userpic.png') }}{% endif %}"/>

另一种优雅的方式是使用默认过滤器:

<img src="{{ app.user.pictureRoute|default('route to userpic.png') }}"/>