Show sub-nodes and current page only in navigation
Hi,
I'm creating a navigation menu and it's nearly there, but I can't work out the last bit.
I have a structure like this:
Home
About Us
Who We Are
The Team
Our History
News
News Archive
Media packs
I would like it so when I click on About us, it shows:
Home
About Us
Who We Are
News
If I click on Who We Are or any of it's nodes, it should show:
Home
About Us
Who We Are
The Team
Our History
News
If I click on News or any of it's nodes, it should show:
Home
About Us
Who We Are
News
News Archive
Media packs
What I have so far is the following, which shows the full structure as in the first example, but I can't work out what I can do to hide the nodes that aren't part of the current page's sub nodes or the current page.
I think you can make use of a property on all IPublishedContent nodes called Path.
If you write out the current Path, in a view you'll get an idea of what it is
@Model.Path
.. it's a comma delimited string of all the unique ids of the content items above the particular item in the content tree. eg
-1,123,1234,343433,5555555
Where 5555555 is the id of the current page, 343433, the id of it's parent, 1234 the id of the section, and so on back to -1 the root of the site etc...
So this means when traversing nodes to write out your navigation, you can 'get' the current section that the page is in using Ancestors and the 'level' (I never know without writing out the levels which would be the number to use for your section page)
Then read the Path property of this section node eg
-1,123,1234
then when looping through the rest of the navigation, if the 'level' is deeper than the 'section' level check the Path property to compare if it starts with -1,123,1234 if it does, then it's a page that's in the current section and should be written out... if it has a different path, you can ignore it...
or at least that's the gist, knowing about the Path property hopefully gives you something to work with to suit your requirements!
That certainly looks interesting. I added it in to have a look at what's produced and can see exactly what you mean about the node structure being accessible.
The logic you've said to apply makes sense for me too. I'll see if I can work out how to get that down as code now.
Show sub-nodes and current page only in navigation
Hi,
I'm creating a navigation menu and it's nearly there, but I can't work out the last bit.
I have a structure like this:
I would like it so when I click on About us, it shows:
If I click on Who We Are or any of it's nodes, it should show:
If I click on News or any of it's nodes, it should show:
What I have so far is the following, which shows the full structure as in the first example, but I can't work out what I can do to hide the nodes that aren't part of the current page's sub nodes or the current page.
Thanks,
Tom
Hi Tom
I think you can make use of a property on all IPublishedContent nodes called Path.
If you write out the current Path, in a view you'll get an idea of what it is
.. it's a comma delimited string of all the unique ids of the content items above the particular item in the content tree. eg
Where 5555555 is the id of the current page, 343433, the id of it's parent, 1234 the id of the section, and so on back to -1 the root of the site etc...
So this means when traversing nodes to write out your navigation, you can 'get' the current section that the page is in using Ancestors and the 'level' (I never know without writing out the levels which would be the number to use for your section page)
Then read the Path property of this section node eg
-1,123,1234
then when looping through the rest of the navigation, if the 'level' is deeper than the 'section' level check the Path property to compare if it starts with -1,123,1234 if it does, then it's a page that's in the current section and should be written out... if it has a different path, you can ignore it...
or at least that's the gist, knowing about the Path property hopefully gives you something to work with to suit your requirements!
regards
marc
That certainly looks interesting. I added it in to have a look at what's produced and can see exactly what you mean about the node structure being accessible.
The logic you've said to apply makes sense for me too. I'll see if I can work out how to get that down as code now.
Thanks,
Tom
is working on a reply...