Skip to main content
Dinu_Arya
Level 6
October 16, 2015
Solved

How to check the size of list in sightly?

[Thread Edited By Adobe]

/*Don’t forget to meet and greet your fellow peers virtually by telling them about yourself here. 

Go ahead and to it now: https://adobe.ly/3eDnB4v */

 

Actual Post:

Hi,

I need to check size of the list in sightly. I need to display UI only when it has more than 2 items in list. Any idea?

Thanks,

Arya.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Feike_Visser1
Feike_Visser1Accepted solution
Adobe Employee
October 16, 2015

Depending on the methods of your list-object you can do something like this:

<div data-sly-test="${yourList.size > 2}">

...

</div>

16 replies

Feike_Visser1
Adobe Employee
Adobe Employee
October 16, 2015

hi arya,

You can just the methods from the object, whether that is List, Array etc.

So if you have .size(), you can use .size. If you have .length() or .count(). You can use .length or .count.

Hope this helps.

Inside your list, you can use things like ${itemList.count}, but this only works inside data-sly-list:

<ul data-sly-list="${currentPage.listChildren}">

<li>${item.name} Page ${itemList.index} of ${itemList.count}</li>

</ul> 

--
Feike

Translate
Parth_Sahu
Adobe Employee
Adobe Employee
March 21, 2020
".size" works for List Objects
Translate
Dinu_Arya
Dinu_AryaAuthor
Level 6
October 16, 2015

Hi Feike,

Thanks. I have gone through sightly tutorials. But I didn't find this attribute. Actually we have size() for java.util.ArrayList. Can I use all of List methods like mylist.<ListMethod> ? Where can I find these tutorials? I have seen only count,first,last,index etc.

 

Thanks,

Arya.

Translate
Parth_Sahu
Adobe Employee
Adobe Employee
April 6, 2021

testing if I can comment!!

Translate
Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

Depending on the methods of your list-object you can do something like this:

<div data-sly-test="${yourList.size > 2}">

...

</div>

Translate
mattm65985934
September 6, 2016

This answer is no longer valid. As far as I can tell, the only way to accomplish this is to create another method in the initialized bean that returns the size of the list.

Translate
Feike_Visser1
Adobe Employee
Adobe Employee
September 7, 2016

It indeed depends what kinds of object you are using for the list object.

Translate
Tyler_Maynard
Level 4
January 19, 2017

I also find it very helpful to use the Ternary Operator in conjunction with the size/length to optionally add classes to an element. Just another front-end tip.

<div class="image-grid-item ${(properties['imageTiles'].length > 3 && rowList.count == 3 ) ? 'image-grid-item_bigger' : ''}"> </div>
Translate
Level 3
September 15, 2017

Hi Felke.

On trying the snippet as below:

    <div data-sly-test="${myList.size > 5}" data-sly-unwrap>

  Heloo!!!!!

        </div>

We get an error as:

Invalid types in comparison. Comparison is supported for Number types only.

Could you please throw some light on why this could be the case?

Thanks,

Hemant Bellani

Translate
Feike_Visser1
Adobe Employee
Adobe Employee
September 15, 2017

What is myList for kind of object?

Translate
Level 3
September 18, 2017

It is an ArrayList object

Translate
Feike_Visser1
Adobe Employee
Adobe Employee
September 18, 2017

Can you share a snippet of your code?

Translate