Is it possible to iterate through an array (or another data type) using HTL? | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
Aug 12, 2021 2:24 AM UTC

Hi @jayv25585659 

You can use the logic in Model class in java and then call the model class in HTL to achieve the use case.

Please see the list feature that HTL provides:

https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#226-list

 

Thanks!

7 replies

Employee
Aug 12, 2021 5:29 AM UTC

Hi @jayv25585659 ,

 

You can create a list using your array in sling model and then in htl using sightly you can iterate over the same

<ul data-sly-list="${model.yourList}">
    <li>${item.property}</li>
</ul>

Hope this helps!!

Thanks 

Translate
jayv25585659
Level 8
Aug 12, 2021 7:29 AM UTC

Are you saying I need to convert my array into a list of objects/model first? Thanks

Translate
Employee
Aug 12, 2021 10:33 AM UTC

@jayv25585659 : Yes, create a list using your array in sling model and then in htl using sightly you can iterate over the same

 

Thanks!!!

Translate
Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
Aug 12, 2021 2:24 AM UTC

Hi @jayv25585659 

You can use the logic in Model class in java and then call the model class in HTL to achieve the use case.

Please see the list feature that HTL provides:

https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#226-list

 

Thanks!

Translate
jayv25585659
Level 8
Aug 12, 2021 7:29 AM UTC

- Are you saying I need to convert my array into a list of objects/model first? Thank you

Translate
Asutosh_Jena_
New Participant
Aug 12, 2021 8:27 AM UTC

@jayv25585659 

Yes. You should convert array into List in Java using the Sling Model and use the model object to iterate it in HTL.

 

Thanks!

Translate