Search code examples
javasling

Apache Sling loop repeating


Does anyone know why the code below is outputting 2 of each navigation element.

//get the full path to the current page
String home = Text.getAbsoluteParent(currentPage.getPath(), 2);    
int absParent = currentStyle.get("absParent", 1);

//checks for invalid and hidden pages.
PageFilter filter = new PageFilter(request);

//utility class that provides an iterator over navigation elements

Navigation nav = new Navigation(currentPage, absParent, filter, 1);

for (Navigation.Element i: nav) {  
%><li <%= i.hasChildren() %>><a href="<%= i.getPath() %>.html"><%= i.getTitle() %></a>      <%
          break;
 }

But if I add in a switch statement within the for loop it displays 1 of each navigation element like it should.

for (Navigation.Element i: nav) {  
     switch (i.getType()) {
     case ITEM_BEGIN:
          %><li <%= i.hasChildren() %>><a href="<%= i.getPath() %>.html"><%=     i.getTitle() %></a><%
          break;
  }
 }

This is driving me crazy, any help is greatly appreciated! Thanks!


Solution

  • you can try this code snippet :

        <%
        Navigation navRoot = new Navigation(currentPage,2,new PageFilter(request),4);
        for (Navigation.Element e: navRoot) {
            switch (e.getType()) {
                case NODE_OPEN:
                %><ul><%
                    break;
                case ITEM_BEGIN:
                    %><li ><a href="<%= e.getPath() %>.html"><%=     e.getTitle() %></a> <%
                    break;
                case ITEM_END:
                %></li><%
                    break;
                case NODE_CLOSE:
                %></ul><%
                    break;
            }
        }
    
        %>
    

    sample component in out of box instance is at location : /apps/geometrixx/components/topnav