Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf conditional img src

I want to show the user avatar if it exists and show a default avatar if not. The code I use is:

<img src="/images/commentavatar1.png" th:src="${comment.user.image} != null ? ${comment.user.image} : '/images/default-user.png'" th:alt="${comment.user.nameSurname}"/>

What I see is only the alt tag. The rendered element has an empty src attribute. Thank you.

like image 490
moonwalker Avatar asked Jan 21 '16 11:01

moonwalker


2 Answers

try

<img th:src="${(comment.user.image != null && !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/>
like image 146
Mykola Yashchenko Avatar answered Oct 26 '22 18:10

Mykola Yashchenko


try.. change && to AND

<img th:src="${(comment.user.image != null && !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/>

to

<img th:src="${(comment.user.image != null AND !#strings.isEmpty(comment.user.image)) ? comment.user.image : '/images/default-user.png'}" th:alt="${comment.user.nameSurname}"/>
like image 24
TopekoX Avatar answered Oct 26 '22 19:10

TopekoX