Let me know if it's buggy, but so far it seems to work AOK for me. If you install the normal CogNav macro, then create a new CSHTML, and swap it over in the macro settings, then it should still work just fine.
You're looking at it :-) install the Cogworks nav package, then copy the above code into a new CSHTML file (Developer > Scripting files), then change the macro settings (under Macros > [COG]Navigation) to use the new file you created.
Re making this the standard for the package instead of the XSLT, that's down to Tim and Adam.
I'm getting some unexpected behaviour (and crashes) when running the code on v6.03. I had a website on 4.11 that used the original XSLT version of your navigation and just switched over to the Razor code. The parameters in my templates are still the same but I'm getting some unexpected behaviour (and crashes). Sadly I have no clue where the crashes occur, since Umbraco does not give any information about that.
However here some info what happens and how my site is built:
Upon visiting the landing page (Home), the navigation does show only the direct children of Home, but all of them carry the "selected" class.
When I now decide to go into the News section the main navigation does not render anymore. (Error loading MacroEngine script (file: [COG]Navigation.cshtml) )
The second-level navigation (code below) still does render everything correctly and only gives me one entry in the sidebar-navigation: "2013", correctly NOT marked as selected yet.
However if I now go into the 2013-folder of my news-section something very interesting happens. The second-level navigation throws an error, not being able to load, BUT the folder "04" (as in the tree the subfolder of 2013) appears now in my MAIN navigation. Also if I now click on it, my News Items show up in the main menu and the sidebar-navigation still shows the error. My main navi should only and always show the direct children of my "Home" node, the second-level navigation everything below these children.
edit:
A little cosmetic idea for the implementation:
change
@foreach(DynamicNode node in parentNode.GetChildrenAsList.Where<DynamicNode>("umbracoNaviHide=false"))
to
@foreach (DynamicNode node in parentNode.GetChildrenAsList.Where<DynamicNode>("Visible")) {
Ok, I sat down today and tried to fix the code ad much as I could.. and I have to say (without wanting to sound rude) that the code was pretty much a-NO-k. In the beginning I wanted to post the lines that I had to change, I redecided to do the more practical solution and post the whole code. A lot had to be cleaned up and smoothed out.
Still I can't guarantee bug-freeness, I'm just happy that I got this far with the given code.
Also I added a functionality which adds a css-class to every link containing the node-id.
Razor version of Flexible nav
Hi guys,
Given the advent of v5, I've found this menu package to be infinitely useful in various different projects.
So, I've ported it to Razor syntax.
How is the performance of your navigation? I've had some problems. You can see the solution here: http://our.umbraco.org/forum/developers/razor/28479-Razor-menu-performance-(v4)?p=3#comment108221
Jeroen
Hiya,
This looks great. Thanks for the contribution. A razor version was on my never shortening list of things to do.
When I get 5mins I'll pull this into the package and take a look at Jeroen's performance concerns.
Tim
Hi Tim,
Are there still plans to have a Razor version of COG Flexible Navigation?
greetings,
Anthony
Hi Anthony,
You're looking at it :-) install the Cogworks nav package, then copy the above code into a new CSHTML file (Developer > Scripting files), then change the macro settings (under Macros > [COG]Navigation) to use the new file you created.
Re making this the standard for the package instead of the XSLT, that's down to Tim and Adam.
HTH,
Benjamin
Hi Benjamin,
I did what you described above, but I'm getting an error:
Error loading MacroEngine script (file: [COG]Navigation.cshtml)
When I open the script in Visual Studio, Intellisense gives red squigly on this line of code:
(line 34) @library.NiceUrl(Model.AncestorOrSelf(1).ID)... (@library is underlined with a red squigly)
and
(line 44) [email protected](node.Id)...(@library is underlined with a red squigly)
I guess it's a namespace thing.
I'm using Umbraco version 4.9.0
greetings,
Anthony
HI Anthony,
Add the following to the top of the file:
HTH,
Benjamin
i am getting this error...
Operator '>' cannot be applied to operands of type 'method group' and 'int'
on this line...
if ((parentNode.ChildrenAsList.Count > 0) || (calculatedMenuDepth == 1 && @Parameter.ForceHome)) {
using version 4.11.3
any thoughts?
okay, so updating to Count() gets rid of errors on save...however, not getting any nodes to render... hmmm
Hiho,
I'm getting some unexpected behaviour (and crashes) when running the code on v6.03. I had a website on 4.11 that used the original XSLT version of your navigation and just switched over to the Razor code. The parameters in my templates are still the same but I'm getting some unexpected behaviour (and crashes). Sadly I have no clue where the crashes occur, since Umbraco does not give any information about that.
However here some info what happens and how my site is built:
My tree looks like this:
Now I call the macro for the main navigation bar like this:
Upon visiting the landing page (Home), the navigation does show only the direct children of Home, but all of them carry the "selected" class.
When I now decide to go into the News section the main navigation does not render anymore. (Error loading MacroEngine script (file: [COG]Navigation.cshtml) )
The second-level navigation (code below) still does render everything correctly and only gives me one entry in the sidebar-navigation: "2013", correctly NOT marked as selected yet.
However if I now go into the 2013-folder of my news-section something very interesting happens. The second-level navigation throws an error, not being able to load, BUT the folder "04" (as in the tree the subfolder of 2013) appears now in my MAIN navigation. Also if I now click on it, my News Items show up in the main menu and the sidebar-navigation still shows the error. My main navi should only and always show the direct children of my "Home" node, the second-level navigation everything below these children.
edit:
A little cosmetic idea for the implementation:
change
to
Jan
Ok, I sat down today and tried to fix the code ad much as I could.. and I have to say (without wanting to sound rude) that the code was pretty much a-NO-k. In the beginning I wanted to post the lines that I had to change, I redecided to do the more practical solution and post the whole code. A lot had to be cleaned up and smoothed out.
Still I can't guarantee bug-freeness, I'm just happy that I got this far with the given code.
Also I added a functionality which adds a css-class to every link containing the node-id.
Here you go:
Jan
Force Home was buggy, there was an extra "s" in the middle of AncestorOrSelf. :)
Bjarni
is working on a reply...