Products

Solutions

Resources

Partners

Community

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

DDRMenu Razor templates

Return to previous page

  • 4/7/2015
  • 21138 Views

Comments

21138 Views

DDRMenu Razor templates

Last updated 5 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
    • Target - The target for for the page (e.g. _blank, Open in New)
    • 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()) + "\""));
                <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

Contents
No sections defined
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out