5

Hi I am new to thymeleaf and am converting ma old project from jsp to thymeleaf.I am trying to convert a piece of code written in jsp which is :

<logic:iterate id="someForm" name="formName" property="nameList" indexId="i">
<%if (i%2==0)
{
 className="even";
 }
 else
 {
  className="odd";
 }
 %>
//some code here

can anyone help me with converting this code in thymeleaf ??

2 Answers 2

14

What you're looking for is in the Thymeleaf documentation. Assuming you need to iterate over your collection to display div tags:

<div th:each="propName,iterStat : ${propNames}" th:class="${iterStat.odd}? 'odd' : 'even'">
    ...
</div
Sign up to request clarification or add additional context in comments.

Comments

2

Just in case your div already has a class and you want to append a new class, use th:classappend:

<div class="col-md-12" th:each="propName,iterStat : ${propNames}" th:classappend="${iterStat.odd}? 'odd' : 'even'">
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.