Search code examples
aemhtl

AEM : How to Iterate a HTL list in reverse order?


Using data-sly-list the elements are printed in the order as they are.

Is there a way in HTL using which we can print the list items in reverse order?

e.g.

if the list contains 1,2,3,4 I want to print 4,3,2,1.


Solution

  • data-sly-list is not currently having control to handle iteration order of list. You would have to change the order in the data you are feeding to list using JavaScript or Java Use API, following is example of JavaScript use API

    In logic.js file

    use(function () {
      var someArrayVar = [1,2,3,4];
      return{
       someArrayRev : someArrayVar.reverse() 
      };
    });
    

    In HTML Markup

     <div data-sly-use.logic="logic.js">
       <div data-sly-list="${logic.someArrayRev}">
           ${item}
       </div>
     <div>