I want to check if an array contains a certain string and render based if its there or not.
testResponse.test1.itemTypes = ['here', 'nothere']
<div th:if="${testResponse!= null AND testResponse.test1.itemTypes == 'here'}">
<ul>
<li><span>I'm here</span></li>
</ul>
You can use the Thymeleaf #arrays.contains()
expression - see the documentation here.
So, for your example it would be:
<div th:if="${testResponse != null
and #arrays.contains(testResponse.test1.itemTypes, 'here')}">
<ul>
<li><span>I'm here</span></li>
</ul>
</div>
Note that the and
has to be lower case. AND
is not valid Thymeleaf syntax.
UPDATE:
For the additional case mentioned in comments, use the not
operator as follows:
<div th:if="${testResponse != null
and not #arrays.contains(testResponse.test1.itemTypes, 'here')}">
<ul>
<li><span>I'm missing</span></li>
</ul>
</div>