Thymeleaf usage

Hello everyone!
I’m trying to use a list in my template, but I need to filter it. Previously I was storing the items in two different arrays: one containing items with images and another one with items that lack images on it. But I found that is possible to have one array but when showing them filtered on the template, but I got some errors when trying to to that. Some of you already tried to use this feature?
ref:
<tr th:each=“artist,rowStat : ${listArtists.?[image == true]}”>

</tr>

Hi PVMerlo

Why don’t you filter the list on the server? Thymeleaf is not very straightforward when it comes to filtering arrays in the template.
If you have one unfiltered list then you can perform a check for image inside the loop when you render the contents, like this:

<tr data-th-each="listArtists : ${listArtist}">
<td>
            <span data-th-if="${listArtist.image}" data-th-remove="tag">
                <img src="${listArtist.image.imageSrc}">
            </span>
</td>
</tr>

Personally, I would use Mustache instead of Thymeleaf, it’s much more straightforward and readable. The code above in Mustache would look like this:

{{#listArtists}}
<tr>

{{#image}}
<td><img src="{{#image.imageSrc}}"></td>
{/image}}

{{^image}}
<td><span>No image</span></td>
{/image}}

</tr>
{{/listArtists}}
2 Likes

Thanks for the answer! For now Mustache is not an option, maybe in next project we adopt it since the beginning, but I agree that it’s more readable :smiley: Since we started with thymeleaf, we never had changed that and it will need more internal alignment to change it, but I’ll bring the discussion to my team.