Expand my Community achievements bar.

SOLVED

Iterate Integer using Sightly

Avatar

Community Advisor

Hi,

I need to iterate a loop based on Integer value. I know a way to do it using jstl but need to do it in sightly.

<c:forEach var="item" begin="1" end="5" varStatus="loop"> </c:forEach>

This loop will get execute 5 times. 

Same I need to do in Sightly.

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi

And

Loop with fixed number of items

<ul data-sly-list="${ [1,2,3,4] }">
<li>${item}</li>
</ul>

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

3 Replies

Avatar

Administrator

Hi 

Please have a look at the documentation:- 

Link:- https://docs.adobe.com/docs/en/aem/6-1/develop/sightly/block-statements.html

//

LIST

data-sly-list: Repeats the content of the host element for each enumerable property in the provided object.

Here is a simple loop:

<dl data-sly-list="${currentPage.listChildren}">
    <dt>index: ${itemList.index}</dt>
    <dd>value: ${item.title}</dd>
</dl>
Code samples are intended for illustration purposes only.

The following default variables are available within the scope of the list:

item: The current item in the iteration.

itemList: Object holding the following properties:

index: zero-based counter (0..length-1).

count: one-based counter (1..length).

first: true if the current item is the first item.

middle: true if the current item is neither the first nor the last item.

last: true if the current item is the last item.

odd: true if index is odd.

even: true if index is even.

//

<ul data-sly-list.child="${currentPage.listChildren}">
  <li class="${ childList.odd ? 'odd' : 'even'}">${child.title}</li>
</ul>

 

//

<div data-sly-list.children="${resource.listChildren}">
    <div data-sly-list.fields="${children.listChildren}">
        <div data-sly-test=${fieldsList.last}> DO SOMETHING BEFORE LAST NODE</div>
        <div data-sly-resource="${fields.path}"></div>
    </div>
</div>

 

So according to your use-case you can use :

<ul data-sly-list.child="${currentPage.listChildren}">
   <li data-sly-test="${childList.index <= 5}">${child.title}</li>
</ul>

I hope this will work for you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Correct answer by
Administrator

Hi

And

Loop with fixed number of items

<ul data-sly-list="${ [1,2,3,4] }">
<li>${item}</li>
</ul>

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Community Advisor

Thanks for your reply. It's working fine in one way :)

But, what I need to achieve is, in case of classic UI with JSTL, what I was doing is, I was passing one integer value from the dialog which I iterated over using jstl code: 

<c:forEach var="item" begin="1" end="${properties.value}" varStatus="loop"> </c:forEach>

Now, how can I use one integer value in sightly to iterate loop as many time of value?

 

Regards,

Himanshu