Iterate list and put into a map in HTL Sightly | Community
Skip to main content
Level 4
July 24, 2024
Solved

Iterate list and put into a map in HTL Sightly

  • July 24, 2024
  • 3 replies
  • 1707 views

Hi Team,

      please find below code 

<sly data-sly-use.fragment="com.adobe.cq.wcm.core.components.models.contentfragment.ContentFragment" data-sly-test="${properties.fragmentPath}" > IN SMB===>> ${fragment.elements.size} <sly data-sly-list.element="${fragment.elements}"> <p>NAME: ${element.name} ::Key==${element.value} </sly> <sly data-sly-use.longplancardstemplate="renderelement.html" data-sly-call="${longplancardstemplate.contentFragment @ element=element}"/> </sly> <sly data-sly-test="${!isParagraphMode}"></sly>

  i want to put ${element.name} and its value ${element.value} in to Map and pass it to renderelement.html as param.

Kindly please help on this.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi @rajat168 
It is not possible to create Map using sightly. you need to write the business logic in sling model,

 

Either you can pass the list and return the Map or extract the ContentFragment in custom sling model, prepare map and return.

3 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 24, 2024

Hi @rajat168 
It is not possible to create Map using sightly. you need to write the business logic in sling model,

 

Either you can pass the list and return the Map or extract the ContentFragment in custom sling model, prepare map and return.

Arun Patidar
h_kataria
Community Advisor
Community Advisor
July 24, 2024

Any particular reason that you are trying to convert it to a Map before passing it to your template ? Essentially you can simply pass the elements itself to the template and can retrieve the name and value in the template itself.

<sly data-sly-use.longplancardstemplate="renderelement.html" data-sly-call="${longplancardstemplate.contentFragment @ fragment=fragment}"/>

And then in your template 

<template data-sly-template.contentFragment="${ @ fragment}"> <sly data-sly-list.element="${fragment.elements}"> name : ${element.name} value : ${element.value} </sly> </template>



rajat168Author
Level 4
July 24, 2024

Hi @h_kataria ,

  when iterating ${fragment.elements},the size was 3 ,its iterating the renderelement.html 3 times and it's leading the duplicate data. I want to iterate ${fragment.elements} at one time and get all data at once

 

 

rk_pandian
Level 4
July 24, 2024

Then its better to iterate the CF elements in a sling model, construct a map and use the map in sightly as per your requirement.

 

Regards,

Ramkumar

kautuk_sahni
Community Manager
Community Manager
August 1, 2024

@rajat168 Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni