I have following expression:
<img th:src="@{/api/file/{imageName}(imageName = ${account.profileImageLink})}" alt="profile picture">
and I want to realize next logic: if imageName equals null so imageName = "profile.jpg"
how can I do that through Thymleaf?
CodePudding user response:
Lots of different ways to accomplish this.
The elvis operator:
<img th:src="@{/api/file/{imageName}(imageName=${account.profileImageLink} ?: 'profile.jpg')}" alt="profile picture">
Conditionals.
<img th:if="${account.profileImageLink != null}" th:src="@{/api/file/{imageName}(imageName = ${account.profileImageLink})}" alt="profile picture"> <img th:unless="${account.profileImageLink != null}" th:src="@{/api/file/profile.jpg}" alt="profile picture">
th:with / ternary:
<img th:with="image=${account.profileImageLink == null ? 'profile.jpg' : account.profileImageLink}" th:src="@{/api/file/{imageName}(imageName=${image})}" alt="profile picture">