how can I iterate over a list of objects in sightly?

19.9k views Asked by At

I have created a Java class method getmyPages() which returns iterator<Page>. Now in HTML page I am able to instantiate the class and access other properties of this class.

However I want to iterate over this iterator the way currentPage.listChildren works.....

Since currentPage.listChildren returns iterator<Page> I am also returning same.

However I am not able to ...... the HTML tag in which I am printing this comes out empty.

3

There are 3 answers

0
Sharath Madappa On

You can use data-sly-list attribute to loop over an iterator. Here's an excerpt from Feike Visser's Sightly tutorials :

<ul data-sly-list.child="${currentPage.listChildren}">
  <li>${child.title}</li>
</ul>

data-sly-unwrap attribute can be used to prevent enclosing tag of the loop from being part of the final mark up

<ul data-sly-list.child="${currentPage.listChildren}" data-sly-unwrap>
  <li>${child.title}</li>
</ul>

Link to the tutorials : http://blogs.adobe.com/experiencedelivers/experience-management/sightly-intro-part-1

Documentation of data-sly-list : link

0
Matt Swezey On

In my Java code I'm returning a List then using Sightly

Java Class

public class ListFilesUse extends WCMUsePojo{

private String name;

private List<TrFile> files = new ArrayList<TrFile>();

Sightly

<div data-sly-use.test="apps.ecolorado.components.sharedcontent.ListFilesUse">
    ${test.name}
    <ul data-sly-list.child="${test.files}">
        <li>${child.fileName}</li>
    </ul>
</div>

And this is working in progress code.

1
shankar On

java class public class TabControl extends WCMUse {

@Override
public void activate() throws Exception {
    // nothing to be done here in this case
}

  public Iterator<Map<String, Object>> getTabTitles(){

      final I18n i18n = new I18n(getRequest());
      List<Map<String, Object>> infos = new LinkedList<Map<String,Object>>();
      String[] tabTitles = getProperties().get("tabs", String[].class);
   if (tabTitles == null || tabTitles.length == 0) {
         tabTitles = new String[1];
         tabTitles[0] = i18n.get("Tab One from sightly");
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("tabTitle",  tabTitles[0]);
         infos.add(map);
   }else if(tabTitles.length > 0){
       for (String tab : tabTitles) {
           Map<String, Object> map = new HashMap<String, Object>();
           map.put("tabTitle",  tab);
           infos.add(map);   
    }
   }
   return  infos.iterator();
}



 }