th:each on numeric values

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

th:each on numeric values

emilime
Hello,

how can I do an iteration like this with Thymeleaf?

<c:forEach var='item' begin='1' end='10'>
   <c:out value='${item}'/>
</c:forEach>

I've found only examples of iterations on model variables (list, arrays)... is it not possible?
Reply | Threaded
Open this post in threaded view
|

Re: th:each on numeric values

Zemi
Administrator
In an enterprise application, you almost always iterate on collections.

To get the desired effect, you can use some trick, like:

      <div th:each="item : ${new int[10]}" th:text="${itemStat.count}"></div>
Reply | Threaded
Open this post in threaded view
|

Re: th:each on numeric values

emilime
I've used a workaround like this but it's not so elegant :)
I think it might be useful to have a way to iterate over a range of numbers (eg: for pagination ;-))
Reply | Threaded
Open this post in threaded view
|

Re: th:each on numeric values

...

This feature will go into 1.1.2 when it is released. See http://www.thymeleaf.org/faq.html#use-snapshot in order to learn how to use a snapshot.

Regards,
Daniel.
danielfernandez
Administrator
Hi,

I've added a #numbers.sequence(from,to) function --returning an Integer[]-- to a new snapshot version of 1.1.2 (version: 1.1.2-SNAPSHOT).

This should allow you to do: