Products

Solutions

Learn More

Partners

Community

Blog

About

DDRMenu Razor templates

Return to previous page

  • 4/7/2015
  • 5987 Views

Comments

5987 Views

DDRMenu Razor templates

Last updated 4 years ago

Comments

Common

(Enter the content of this article below)

Advanced

 
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 ID
    • Text - The page name (i.e. what should normally be displayed in the menu)
    • Title - The full page title
    • Url - The page URL
    • Enabled - Whether the page is enabled
    • Selected - Whether the page is selected
    • Breadcrumb - Whether the page is in the current breadcrumb
    • Separator - Whether the node is a separator
    • Icon - The URL of the page icon
    • LargeImage - The URL of the large page icon (DNN 6 only)
    • First - Whether the page is the first in its level
    • Last - Whether the page is the last in its level
    • Depth - The depth of the current page in the menu structure (starting at 0)
    • Keywords - The keywords defined for the current page
    • Description - The description of the current page
    • CommandName - The action command name (action menus only)
    • CommandArgument - The action command argument (action menus only)
    • Children - The child nodes of this node
    • Parent - The parent node of this node
  • ControlID - The ASP.NET control ID of the current DDRMenu instance
  • Options - The client options (in JSON format)
  • DNNPath - The path of the DNN application root
  • ManifestPath - The path of the menu template's manifest folder
  • PortalPath - The path to the current portal root
  • SkinPath - 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()) + "\""));
<[email protected]>
@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

Contents
No sections defined
DNN Launch Webinar
VIEW ON-DEMAND
DNN Launch Webinar
VIEW ON-DEMAND
DNN Launch Webinar
VIEW ON-DEMAND