AEM Sightly iterate through list using data-sly-list

Sightly list iteration

In this article we will learn how we can iterate through the resouce list and create our frontend, according to sightly documentation of resource block, so supose if i want to create a slider, you first need the sightly responce and after then you will be able to itrate the list in sightly so lets create a simple xml component where user can add the data and then we will iterate list in aem sightly, let now create a slider in our _cqdialog.xml .

In above code we have created history module where we have added items field and in item we defiled different names so while authoring user can add multiple items or no of slider, now we will use these attributed to fetch data on html sighlty, now lets write below sightly code to fetch the component data wath user authored

in above code we are using resource method to iterated between the lists in sightly, we are storing the resource data in historyDetials and in next sly tag we are iterating that so the div inside the ${historyDetials.listChildren} will be repeated multiple time as the no of list available in resouce as data and to fetch the perticuler data form the object we will attached the name with the object as shown above ${item.valueMap.watchTitle}

there ase more more oprations we can apply on the resource listing in sightly as below

To check the last item on the list

to get the total no of lists
So if you have .size(), you can use .size. If you have .length() or .count(). You can use .length or .count.

To get the current index of the iterated list use the below method

In above code you can also apply conditions in you want to iterate certain limit

Further reading

  • React UseEffect | simplifyScript

    Understanding the useEffect hook in React

    In this article, we will learn about another React hook called useEffect(), through this article you will be able to understand what is react useEffect Hook and how to use useEffect() hook in react, this is most communally used hook after useState() hook, we mostly use this hook in every component on react now first […]
  • React useState function

    How to Use useState Hook in React Application

     In a React application, normal vanilla JavaScript may not work for some concepts. For example, if I want to toggle a class on a button click or add and remove classes, I might need to show a popup on the website. This can be easily achieved in vanilla JavaScript using the element.classList.toggle(“newClass”) method, allowing us to add […]
  • javascript DOm manipulation - simplifyscript

    What is DOM manipulation in javascript 

    In this article, we will explore JavaScript DOM manipulation, a key feature of JavaScript that allows developers to modify the Document Object Model (DOM) of a webpage. DOM manipulation enables you to dynamically alter the content and structure of HTML documents. To grasp this concept thoroughly, we’ll start by defining what the DOM is and […]