Iteration (for loop) in sightly

Manisha picture Manisha · May 20, 2016 · Viewed 29.5k times · Source

I had used <c:forEach> in jstl. Now i want to use sightly.

My usecase is to print numbers from 1 to 10 then how can i iterate in sightly as for loop in java

Answer

CptBartender picture CptBartender · May 20, 2016

Sightly doesn't let you put any logic in it by design. What you should do is, you should customize your underlying model so that you can retrieve a prepared list of irrelevant (from Sightly's perspective) length that contains data that needs to be displayed. After that, just use data-sly-list. You'll need to google for more details, but in general, this is how you use lists in Sightly:

<ul data-sly-list.myitem="${mymodel.myitems}" data-sly-unwrap>
  <li>${myitem.foo}</li>
</ul>

EDIT: As pointed out by @nateyolles, you can iterate over a specific range of items by using the index in a test condition (<li data-sly-test="${itemList.count >= 2 && itemList.count <= 6}">${item}</li>), though I am not sure if this is the "Sightly way". Personally, I would advise not to, unless it would make your life noticeably easier.