0
votes

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!

2

2 Answers

3
votes

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

0
votes

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.