This page describes how the DDRMenu Razor template processor works. Razor-based templates provide most power, including access to the DotNetNuke API. Starting with DotNetNuke 7.0, support is built in in the platform, but requires DDRMenu 2.0.3 or newer to work properly. This is due to a breaking change in DotNetNuke 7, which is explained in this blog post.
In older versions of DotNetNuke DDRMenu Razor templates are only supported with .NET 4.0 and the Razor Host module installed.
Data model
Razor templates receive the following members in the Razor model:
Source.root
- The root menu node. You can see the exact structure of MenuNode in the DDRMenu source code, but in summary the following properties are available:
TabId
- The page IDText
- The page name (i.e. what should normally be displayed in the menu)Title
- The full page titleUrl
- The page URLTarget
- The target for for the page (e.g. _blank, Open in New)Enabled
- Whether the page is enabledSelected
- Whether the page is selectedBreadcrumb
- Whether the page is in the current breadcrumbSeparator
- Whether the node is a separatorIcon
- The URL of the page iconLargeImage
- The URL of the large page icon (DNN 6 only)First
- Whether the page is the first in its levelLast
- Whether the page is the last in its levelDepth
- The depth of the current page in the menu structure (starting at 0)Keywords
- The keywords defined for the current pageDescription
- The description of the current pageCommandName
- The action command name (action menus only)CommandArgument
- The action command argument (action menus only)Children
- The child nodes of this nodeParent
- The parent node of this node
ControlID
- The ASP.NET control ID of the current DDRMenu instanceOptions
- The client options (in JSON format)DNNPath
- The path of the DNN application rootManifestPath
- The path of the menu template's manifest folderPortalPath
- The path to the current portal rootSkinPath
- The path to the current skin
You can see an example of the values these take in the
output of the DumpXML template).
Example
A simple example (using a .cshtml C# template) might look like this (this is the DotNetNuke 7.x + syntax):
@using DotNetNuke.Web.DDRMenu;
@using System.Dynamic;
@inherits DotNetNuke.Web.Razor.DotNetNukeWebPage<dynamic>
@{ var root = Model.Source.root; }
@helper RenderNodes(IList<MenuNode> nodes) {
if (nodes.Count > 0) {
<ul>
@foreach (var node in nodes) {
var cssClasses = new List<string>();
if (node.First) { cssClasses.Add("first"); }
if (node.Last) { cssClasses.Add("last"); }
if (node.Selected) { cssClasses.Add("selected"); }
var classString = new HtmlString((cssClasses.Count == 0) ? "" :
(" class=\"" + String.Join(" ", cssClasses.ToArray()) + "\""));
<li @classString>
@if (node.Enabled) {
<a href="@node.Url">@node.Text</a>
} else {
@node.Text
}
@RenderNodes(node.Children)
</li>
}
</ul>
}
}
@RenderNodes(root.Children)
If you are using DotNetNuke 5.x or 6.x, the syntax would be as follows:
@using DotNetNuke.Web.DDRMenu;
@{ var root = Model.Source.root; }
@helper RenderNodes(IList<MenuNode> nodes) {
if (nodes.Count > 0) {
<ul>
@foreach (var node in nodes) {
var cssClasses = new List<string>();
if (node.First) { cssClasses.Add("first"); }
if (node.Last) { cssClasses.Add("last"); }
if (node.Selected) { cssClasses.Add("selected"); }
var classString = new HtmlString((cssClasses.Count == 0) ? "" :
(" class=\"" + String.Join(" ", cssClasses.ToArray()) + "\""));
<li@classString>
@if (node.Enabled) {
<a href="@node.Url">@node.Text</a>
} else {
@node.Text
}
@RenderNodes(node.Children)
</li>
}
</ul>
}
}
@RenderNodes(root.Children)
Related content
Additional links