How to check the size of list in sightly? | Community
Skip to main content
Dinu_Arya
Level 6
Oct 16, 2015 2:28 AM UTC
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
Employee
Oct 16, 2015 2:28 AM UTC

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

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

...

</div>

16 replies

New Participant
Apr 13, 2023 7:22 AM UTC

We can create a variable like this if list size is small, e.g. total is a variable created here which can be used further where ever size is required we can use this total object

<sly data-sly-list.listObj="${object.mylist}">
          <sly data-sly-test="${listObjList.last}">
            <sly data-sly-test.total="${listObjList.count}" />
      </sly>
</sly>

for example if I need to test if size is 2. We can use statement like 

data-sly-test="${total>2}"

Translate
Feike_Visser1
Employee
Sep 18, 2017 11:23 AM UTC

Use: ${myWCMUseObject.allColumns.size}

Translate
Level 3
Sep 18, 2017 11:21 AM UTC

// The WCMUse class' method that gives me an ArrayList

public List<String> getAllColumns() {

  List<String> allColumns = new ArrayList<String>();

  for (int i = 1; i <= 10; i++) {

    allColumns.add("Test" + "-" + i);

  }

  return allColumns;

  }

 

  # sightly code that I am trying out ....

<sly data-sly-list.myList="${myWCMUseObject.allColumns}">

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

  Heloo!!!!!

        </div>

</sly>

Translate
Level 2
Dec 8, 2022 1:53 PM UTC

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

Give like the above it will work..

Translate
Feike_Visser1
Employee
Sep 18, 2017 8:00 AM UTC

Can you share a snippet of your code?

Translate
Level 3
Sep 18, 2017 2:30 AM UTC

It is an ArrayList object

Translate
Feike_Visser1
Employee
Sep 15, 2017 10:44 AM UTC

What is myList for kind of object?

Translate
Level 3
Sep 15, 2017 10:32 AM UTC

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
Tyler_Maynard
Level 4
Jan 19, 2017 8:26 PM UTC

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
Feike_Visser1
Employee
Sep 7, 2016 7:30 AM UTC

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

Translate
mattm65985934
New Participant
Sep 6, 2016 4:26 PM UTC

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