简体   繁体   中英

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!

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

I do not know the exact reason for 2 of each navigation element. But CQ5 documentation on " Navigation " states that

"A navigation element reflects a page and can have different Navigation.Element.Types . Note that the same page might be returned 4 times for the different element types. this offers maximal flexibility when drawing the navigation."

Possibly the same element is being returned 2 times for element types. If you put the switch-case block you are selecting a particular element type and hence shows only once.

Perhaps the key to your answer lies in Navigation.Element.Types .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM