0

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!

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
Delmon Young
  • 2,013
  • 5
  • 39
  • 51

2 Answers2

3

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

K S
  • 46
  • 3
0

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.

AniJ
  • 205
  • 2
  • 4
  • 9
  • thanks @anij it just seems like redundant code to add in the switch statement. do you know if there's another method to capture top level pages. a code sample would really help i'm a complete newb to CQ5. – Delmon Young Apr 03 '13 at 01:12
  • @Delmon Young Are you trying to build a "Top Navigation" component? Is that your problem statement. If yes you can look at the code for existing samples sites that come along with a CQ5 installation. The navigation component is present there. I will try and post the code later but you can definitely check out the samples. In "crxde" you can look in /libs/foundation/components (i think thats the path) or under /apps//components . – AniJ Apr 03 '13 at 14:37
  • Thanks @anij the code from above is a modification from the sample sites that come with a CQ5 installation. I was trying to get a better understanding of why they function the way they do. If you have a code example that would be very helpful. – Delmon Young Apr 04 '13 at 13:03