for loop in thymeleaf

user1275645 picture user1275645 · Dec 17, 2013 · Viewed 35.7k times · Source

How can I do the following (java):

for(int i = 0; i < 81 ; i+=20){
   //Should loop through 5 times!
}

in Thymeleaf?

I've tried this:

<option th:each="i : ${#numbers.sequence( 1, 81/20)}">
   <p th:text="${ i }"></p> <!-- THIS loops 4 times, instead of 5 -->
</option>

The problem is that it is not as accurate as the java piece of code. How to do this?

Answer

sitakant picture sitakant · Dec 17, 2014

use iterStat key word to iterate. Example If you have an Array of String and you are iterating the same using thymeleaf.

<div th:each="str,iterStat : strings">
   <span th:text="${str}"/><!--This will print the index value-->
   <span th:text="${iterStat.index}"/><!--This will print the Index-->
</div>