It works okay, but if i try to add my own conditionals (such as my logic for the home page), it breaks. Every time.
Is there a rule for Razor that i'm missing regarding conditionals? Does anybody have a good link for a general intro to Razor that is relevant to the Umbraco context?
My code:
@*
NESTED NAVIGATION WITH START AND FINISH LEVELS
=======================================
This snippet makes it easy to do navigation based lists! It'll automatically produce a nested list all children of a page within certain
levels in the hierarchy that's published and visible (it'll filter out any pages with a property named "umbracoNaviHide"
that's set to 'true'.
Based on the inbuilt Navigation script in Umbraco 4.7
How to Customize for re-use (only applies to Macros, not if you insert this snippet directly in a template):
- If you add a Macro Parameter with the alias of "StartLevel" you can define the starting level for which nodes will be displayed
- If you add a Macro Parameter with the alias of "FinishLevel" you can define the finish level for which nodes will be displayed
Okay... lets put this down to a learning curve issue. I"ve fixed it now.. it basically took rewriting the thing about 5 times, each time improving and understanding more and more...
I'm not sure exactly what was wrong with the above code - I think it was a combination of various things.. here's what i came up with if anybody is interested (or cares to offer suggestions for improvement)?
*@
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@{
var startLevel = String.IsNullOrEmpty(Parameter.StartLevel) ? 2 : int.Parse(Parameter.StartLevel);
var finishLevel = String.IsNullOrEmpty(Parameter.FinishLevel) ? 5 : int.Parse(Parameter.FinishLevel);
Razor driving me crazy... hopefully a simple mistake
I'm a newbie to Razor, so hopefully this is just a simple Razor rule i'm not aware of or something... So here goes:
I'm trying to create a navigation based on one i found on the forum previously
It works okay, but if i try to add my own conditionals (such as my logic for the home page), it breaks. Every time.
Is there a rule for Razor that i'm missing regarding conditionals? Does anybody have a good link for a general intro to Razor that is relevant to the Umbraco context?
My code:
Thanks heaps
Greg
I should mention i'm using Umbraco 4.7.1.1
Hi. I guess that your "AssignHomeManually" macro parameter is of the "bool" type, and so you need to write
instead of
Okay... lets put this down to a learning curve issue. I"ve fixed it now.. it basically took rewriting the thing about 5 times, each time improving and understanding more and more...
I'm not sure exactly what was wrong with the above code - I think it was a combination of various things.. here's what i came up with if anybody is interested (or cares to offer suggestions for improvement)?
*@
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@{
var startLevel = String.IsNullOrEmpty(Parameter.StartLevel) ? 2 : int.Parse(Parameter.StartLevel);
var finishLevel = String.IsNullOrEmpty(Parameter.FinishLevel) ? 5 : int.Parse(Parameter.FinishLevel);
dynamic homeNode = Model.NodeById(Parameter.HomeNode);
<divid="nav">
<ulid="topnav"class="sf-menu">
@if (Parameter.AssignHomeManually == "1")
{
var selected = Model.Id == homeNode.Id ? " class=\"current\"" : "";
<li><a @Html.Raw(selected) href="/">HOME</a></li>
}
@RenderChildren(Model.AncestorOrSelf().Children.Where("Visible"), false, startLevel)
</ul>
</div>
}
@helper RenderChildren(dynamic childNodes, bool isNested, int startLevel)
{
DynamicNode currentMenu = @Model.AncestorOrSelf(startLevel);
Html.Raw(isNested ? "<ul>" : "");
foreach (var node in childNodes)
{
var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";
<li><a @Html.Raw(selected) href="@node.Url" title="@node.Name" >@node.Name</a></li>
@RenderChildren(node.Children, true, startLevel)
}
Html.Raw(isNested ? "</ul>" : "");
}
Whoa that was a bit weird.. seems copying from windows and pasting into mac does some strange stuff (ie: the colour scheme).
Also there was no code formatting option in the forum.. strangeness
is working on a reply...