DDRMenu Razor Template Engine
DNN Version: 09.02.00
• 4 minutes to read
• 4 minutes to read
This page describes how the DDRMenu Razor template processor works. Razor-based templates provide most power, including access to the DNN API. Starting with DNN 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 DNN 7, which is explained in this blog post.
Note
In older versions of DNN, DDRMenu Razor templates are only supported with ASP.NET 4.0 with 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 theme
Example
A simple example (using a .cshtml C# template) might look like this (this is the DNN 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)