可用时显示用户图片


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') }}"/>