BD0337E94A6D4BE5875DB6BC5BD35CE8
  • RedDot CMS Blog
  • 21.02.2020
  • EN

rdb: Skipping levels in the navigation

I’ve been working on a client’s project over the last few weeks and have learnt a neat little trick from the site’s original developer that I thought I’d share with you all.

There may come a time in your life when there are certain pages in a site’s structure that you really don’t need, or want, the site’s users to go to in order to streamline the site a little. A good example could be a Press archive that is broken down into years.

Imagine your site structure looks something like this :

  • Home
    • Press
      • 2009
      • 2008
      • 2007

When we click on Press we want to jump straight to 2009 rather than having to go to the Press page and then click down again to get to 2009. In this example we’re going to check for an option list to see if we need to skip 1 level.

<htmltext><li><a href="</htmltext>
<if>
  <query valuea="Context:CurrentIndex.Page.Elements.GetElement(opt_Navigation).GetHtml()" operator="==" valueb="Skip 1 level"> 
    <htmltext>
      <%!! Context:CurrentIndex.SubIndexes[Int32:0].GetUrl() !!%>"><%!! Context:CurrentIndex.Headline !!%></a></li>
    </htmltext> 
  </query> 
  <query type="else">
    <htmltext>
      <%!! Context:CurrentIndex.GetUrl() !!%>"><%!! Context:CurrentIndex.Headline !!%></a></li>
    </htmltext>
  </query>
</if>

That’s all that’s needed to skip over a page. Where you see Context:CurrentIndex.SubIndexes[Int32:0] we are basically telling navigation manager to have a look at the current position in the structure and look for it’s first child. This means that the page you want to skip to needs to be at the top of the navigation list. To jump to a different page in the list, just adjust the number in SubIndexes from 0 to something else.

There is one important thing to note at this point. When you’re dealing with a navigation manager project, you always need to present the entire site structure if you intend to publish the entire site in one go (using Publish with all following pages). If we look at the tree structure again with the published pages in bold. If we skip from Home directly to 2009 you’ll notice that suddenly 2008 and 2007 aren’t children of any published pages… so they won’t publish either.

  • Home
    • Press
      • 2009
      • 2008
      • 2007

This was the point at which we tried publishing “all related pages”. Don’t try this one folks. Really. Yes, some of your missing pages will publish, but the system no longer know where they are in the navigation.

“Why should this matter? Surely they publish, so that’s ok right?” Well yes, the pages do publish, but because they are, as far as navigation manager is concerned, unrelated to anything in the site’s structure, the navigation scripts and render tags all fail. You’re then left with no navigation on these pages… not good.

So instead, we need to let Navigation manager know that these pages are in the system and would it be so kind as to actually publish them rather than cause me to get more grey hairs at my tender, young age. To do this there is a simple little trick we can do; we add a commented out url to the page we are skipping. This fools Navigation Manager into following the comment to these extra pages and publishes them.

<htmltext><li><a href="</htmltext> 
<if> 
  <query valuea="Context:CurrentIndex.Page.Elements.GetElement(opt_skipInNavigation).GetHtml()" operator="==" valueb="Skip 1 level"> 
    <htmltext>
      <%!! Context:CurrentIndex.SubIndexes[Int32:0].GetUrl() !!%>"><%!!Context:CurrentIndex.Headline!!%></a>
      <!-- <%!!Context:CurrentIndex.GetUrl()!!%> --></li>
    </htmltext> 
  </query> 
  <query type="else"> 
    <htmltext>
      <%!! Context:CurrentIndex.GetUrl() !!%>"><%!! Context:CurrentIndex.Headline !!%></a></li>
    </htmltext> 
  </query>
</if>

I’m outputting it as an HTML comment here for simplicity’s sake. If you’re preexecuting your pages you can turn this into an ASP/PHP/Coldfusion /whatever comment instead so you don’t see anything in the source code for the published pages.

It’s a simple, quick solution now you’re aware of it, but one that will save you no end of grief.

What happens if you want to go even deeper into the project structure?
 

One step beyond…

So. Let’s imagine that you’ve started to put out so many press releases that you’ve had to break down your archive into months as well as years. Now your boss want to jump straight to the latest month rather than simply go to 2009. We can take the same code and expand it slightly to cope with this new requirement.

<htmltext><li><a href="</htmltext> 
<if>
  <query valuea="Context:CurrentIndex.Page.Elements.GetElement(opt_skipInNavigation).GetHtml()" operator="==" valueb="Skip 2 levels">
    <htmltext>
      <%!! Context:CurrentIndex.SubIndexes[Int32:0].SubIndexes[Int32:0].GetUrl() !!%>"><%!!Context:CurrentIndex.Headline!!%></a>
      <!-- <%!!Context:CurrentIndex.GetUrl()!!%> --></li>
    </htmltext> 
  </query> 
  <query type="else">
    <htmltext>
      <%!! Context:CurrentIndex.GetUrl() !!%>"><%!! Context:CurrentIndex.Headline !!%></a>
    </htmltext> 
  </query>
</if>

All we’re doing is adding in another .SubIndexes[Int32:0], which tells the script to find the first child of the first child. Notice we’ve still got the comment in there for safety sake. In theory you could keep going, though to be honest I’ve not tried it any further than this.

Adrian has previously written a similar script for skipping over pages, but his was automatic rather than relying upon a manually set option list, If he wants to explain this to us, that would be cool. Otherwise you’ve now got an idea for expanding this as your own pet project ;)

If you have an idea for an article you want us to write, or even better, one you’ve written yourself, drop us an email and we can have a chat.

       

Downloads

 

QuickLinks

 

Channel