Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I have written the following script which works without issue:
@inherits umbraco.MacroEngines.DynamicNodeContext@{ var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level); var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : " class=\"current\""; var parent = @Model.AncestorOrSelf(level); if (parent != null) { <ul class="menu"> @if (parent.Id == Model.Id) { <li class="menu-item-selected"><a href="@parent.Url">Home</a></li> } else { <li><a href="@parent.Url">Home</a></li> } @foreach (dynamic item in parent.Children.Where("Visible").OrderBy("Order")) { <li> if (item.Id == Model.Id) { <a href="@item.Url">@item.Name</a> } else { <a href="@item.Url">@item.Name</a> } @if (item.Children.Count() > 0) { @traverse(item) } </li> } </ul> }}
However I was wanting to extend it the foreach loop to allow the class "menu-item-selected" to be added to the <li> tag.
I have changed the script to be like this:
@foreach (dynamic item in parent.Children.Where("Visible").OrderBy("Order")) { if (item.Id == Model.Id) { <li class="menu-item-selected"><a href="@item.Url">@item.Name</a> } else { <li><a href="@item.Url">@item.Name</a> } @if (item.Children.Count() > 0) { @traverse(item) } </li> }
However it won't let me save the script and states the issue "The code block is missing a closing "}" character". Am I missing an @ somewhere, if so could somebody cast their eyes over it and advise where as i've been stumped for an hour now!
Thanks
In Razor you need to close a HTML tag inside the same code block.
For example:
@if(something == true) { <li><a href="index.html">Home</a> } </li>
The above will break.
However:
@if(something == true) { <li><a href="index.html">Home</a></li> }
The above will work.
If you want to output a tag but don't want the open or close tag to break the code, then do:
@Html.Raw("<li>")
Any questions let me know.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
hierarchical menu issue
I have written the following script which works without issue:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : " class=\"current\"";
var parent = @Model.AncestorOrSelf(level);
if (parent != null)
{
<ul class="menu">
@if (parent.Id == Model.Id)
{
<li class="menu-item-selected"><a href="@parent.Url">Home</a></li>
}
else
{
<li><a href="@parent.Url">Home</a></li>
}
@foreach (dynamic item in parent.Children.Where("Visible").OrderBy("Order"))
{
<li>
if (item.Id == Model.Id)
{
<a href="@item.Url">@item.Name</a>
}
else
{
<a href="@item.Url">@item.Name</a>
}
@if (item.Children.Count() > 0)
{
@traverse(item)
}
</li>
}
</ul>
}
}
However I was wanting to extend it the foreach loop to allow the class "menu-item-selected" to be added to the <li> tag.
I have changed the script to be like this:
@foreach (dynamic item in parent.Children.Where("Visible").OrderBy("Order"))
{
if (item.Id == Model.Id)
{
<li class="menu-item-selected"><a href="@item.Url">@item.Name</a>
}
else
{
<li><a href="@item.Url">@item.Name</a>
}
@if (item.Children.Count() > 0)
{
@traverse(item)
}
</li>
}
However it won't let me save the script and states the issue "The code block is missing a closing "}" character". Am I missing an @ somewhere, if so could somebody cast their eyes over it and advise where as i've been stumped for an hour now!
Thanks
In Razor you need to close a HTML tag inside the same code block.
For example:
The above will break.
However:
The above will work.
If you want to output a tag but don't want the open or close tag to break the code, then do:
Any questions let me know.
is working on a reply...