2

I got the following line

<img data-language="de" th:onclick="'languageSwitch(this)'" src="/productfinder/images/blank.png" th:class="flag flag-de ${languageFilter.de} ? 'flag-active' : 'flag-disabled'" alt="Language Flag" />

Throws me:

Could not parse as expression: "flag flag-de ${languageFilter.de} ? 'flag-active' : 'flag-disabled'"

Not quite sure what Im doing wrong here.

Xatenev
  • 6,383
  • 3
  • 18
  • 42
  • see http://stackoverflow.com/questions/13494078/how-to-do-if-else-in-thymeleaf –  Aug 10 '16 at 14:39
  • @RC. Thanks for the link. Tried that first solution, doesnt seem to work, no idea how to use that on my string .... – Xatenev Aug 10 '16 at 14:41

1 Answers1

5

You need to use single quotes for string literals and concatenate the literal to the class name returned by if-else, so

<img data-language="de" th:onclick="'languageSwitch(this)'" src="/productfinder/images/blank.png" th:class="'flag flag-de ' + ${languageFilter.de} ? 'flag-active' : 'flag-disabled'" alt="Language Flag" />

There is also available an attribute called th:classappend.

<img data-language="de" th:onclick="'languageSwitch(this)'" src="/productfinder/images/blank.png" th:class="flag flag-de" th:classappend="${languageFilter.de} ? 'flag-active' : 'flag-disabled'" alt="Language Flag" />

Documentation here.

Rahul Sharma
  • 892
  • 1
  • 6
  • 16