16

Is there a way to use the Java instanceof operator in Thymeleaf?

Something like:

<span th:if="${animal} instanceof my.project.Cat" th:text="A cat"></span>
<span th:if="${animal} instanceof my.project.Dog" th:text="A dog"></span>
Mahozad
  • 18,032
  • 13
  • 118
  • 133
Andrea
  • 15,900
  • 18
  • 65
  • 84

1 Answers1

27

Try:

<span th:if="${animal.class.name == 'my.project.Cat'}" th:text="A cat"></span>

or, if using Spring:

<span th:if="${animal instanceof T(my.project.Cat)}" th:text="A cat"></span>

more about using SpEL and dialects in thymeleaf.

Trynkiewicz Mariusz
  • 2,722
  • 3
  • 21
  • 27