Skip to main content

Navigation in DD4T

There always been a question on What is the best approach for building navigation with DD4T and ASP.NET MVC?
Answer is: There is TridionSiteMapProvider as part of the DD4T.Mvc project, which can be used out of the box. The  DD4T.Examples.Templates project also have a example on how Sitemap can be generated from the template (TBB)
Many organization will have different kinds of navigation structure, It can be from very simple structure or it can be a heavily complex in nature. In this article you can see how the default SitemapProvider can be used and how we could customize to achieve complex Navigations like Megamenu or similar using TridionSiteMapProvider.

Setting up the default TridionSiteMapProvider

The only configuration you would need to do in web.config is set the your default sitemap provider is as below.
<system.web>
  <siteMap defaultProvider="TridionSiteMapProvider" enabled="true">
    <providers>
      <clear/>
      <add name="TridionSiteMapProvider" type="DD4T.Mvc.Providers.TridionSiteMapProvider, DD4T.Mvc"/>
    </providers>
  </siteMap>
</system.web>

By default the sitemap path url looks in /system/sitemap/sitemap.xml, or based on web config.

In the example project, you have an Html extension to render the sitemap, it will return just the Html list. This helper can be called as @Html.SiteMapMenu(MenuType.Top)

In simple, it works as below diagram.


Customizing the default TridionSiteMapProvider

The TridionSiteMapProvider out of box is handling most of the scenarios; Sometime when we work with Mega menu or complex navigation, we might have the situation to handle more data other than normal sitemap node attributes. Below I am intent to explain how we could customize the TridionSiteMapProvider.
I have a custom TBB which generate a sitemap.xml or navigation.xml looks something like below by navigating the structure.

<root>
  <node id="tcm:8-4450-4" title="010. Personal" url="/en/personal/" description="tcm:8-337-64" compTitle="Personal Site" vanityURL="">
   <navTitle>Personal</navTitle>
    <NavConf>
      <NavUseFulLinks>
        <UseFulLinks>
          <Links>
            <Title>Utility Links for Personal</Title><TitleComponentLink>tcm:8-1224</TitleComponentLink>
            <Links>
              <LinkText>Some Text</LinkText>
              <ComponentLink>tcm:8-1000</ComponentLink>
            </Links>
            <Links>
              <LinkText>Some title text</LinkText>
              <ComponentLink>tcm:8-1018</ComponentLink>
        </Links>
            <Links>
              <LinkText>Some Title Text</LinkText>
              <ComponentLink>tcm:8-1950</ComponentLink>
            </Links></Links>
        </UseFulLinks>
      </NavUseFulLinks>
    </NavConf>
  </node>
</root>

In above xml, I have additional Node named NavConf with some more utilities links, which are obtained from Page Metadata. In such scenarios we could add the custom property to TridionSiteMapNode and then we can customize our sitemap helper to return the custom view template based on request.

In above scenario I have add a new Property named NavConf, in the class TridionSiteMapNode, and then In the TridionSiteMapProvider.cs class, I have customized the CreateNodeFromElement method to looks like below,
SiteMapNode childNode = new TridionSiteMapNode(this,
element.Attribute("id").Value, //key
uri,
element.Attribute("url") != null ? element.Attribute("url").Value : "", //url
element.Attribute("title") != null ? element.Attribute("title").Value : "", //title
element.Attribute("description") != null ? element.Attribute("description").Value : "", //description
null, //roles
attributes, //attributes
null, //explicitresourceKeys
null
)
{
Level = currentLevel,
NavConf = element.Element("NavConf") != null ? element.Element("NavConf") : null
}; 

The default navigation Helper class can be customized more generic so that we can customize the html in the view rather than in the Helper class. The extension CreateHtmlHelperForModel below will return the template view from the shared template folder based on the request e.g: Top, Left or Full. Only concern is you should have the below views on the Shared folder, because the Template will look for the view in DisplayTemplate Folder.

Creating NavigationHelper class


  public static class NavigationHelper
  {
      
        public static MvcHtmlString Navigation(this HtmlHelper helper,SiteMapNode rootNode, IPage page, NavigationTypes navigationType, string navigationXml = "/system/include/navigation.xml")
        {
            var langauge = helper.ViewContext.RouteData.Values["language"] as string;
            navigationXml = string.Format("/{0}/{1}", langauge, "system/include/navigation.xml");
            string template = string.Empty;
            switch (navigationType)
            {
                case NavigationTypes.Top:
                    template = "TopNavigation";
                    break;
                case NavigationTypes.Left:
                    template = "LeftNavigation";
                    break;
                case NavigationTypes.Right:
                    template = "RightNavigation";
                    break;
            }

         //   XmlDocument model = Utilities.GetXmlFile(navigationXml);

            var navigationHelper = new NavigationHtmlHelper(helper);
            return navigationHelper.CreateHtmlHelperForModel(rootNode).DisplayFor(x => rootNode, template,
                new
                {
                    NavParent = page.StructureGroup.Id,
                    PageUri = page.Id,
                    PublicationUri = page.Publication.Id
                }
            );
        }
    }

    public enum NavigationTypes
    {
        Top,
        Left,
        Bottom,
        Right,
        Breadcrum,
        Title,
        Canonical
    }

    public class NavigationHtmlHelper
    {
        /// <summary>
        /// Gets or sets the HTML helper.
        /// </summary>
        /// <value>The HTML helper.</value>
        public HtmlHelper HtmlHelper { get; protected set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MvcSiteMapHtmlHelper"/> class.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="provider">The sitemap.</param>
        public NavigationHtmlHelper(HtmlHelper htmlHelper)
        {
            if (htmlHelper == null)
                throw new ArgumentNullException("htmlHelper");

            HtmlHelper = htmlHelper;

        }

        /// <summary>
        /// Creates the HTML helper for model.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public HtmlHelper<TModel> CreateHtmlHelperForModel<TModel>(TModel model)
        {
            return new HtmlHelper<TModel>(HtmlHelper.ViewContext, new ViewDataContainer<TModel>(model));
        }
    }

    public class ViewDataContainer<TModel>
       : IViewDataContainer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewDataContainer&lt;TModel&gt;"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        public ViewDataContainer(TModel model)
        {
            ViewData = new ViewDataDictionary<TModel>(model);
        }

        /// <summary>
        /// Gets or sets the view data dictionary.
        /// </summary>
        /// <value></value>
        /// <returns>The view data dictionary.</returns>
        public ViewDataDictionary ViewData { get; set; }
    }
In the view, we can have something like below as simple or as complex as based on requirement.

@model TridionSiteMapNode
@if (Model.HasChildNodes)
{
    <ul>
        @foreach (TridionSiteMapNode node in Model.ChildNodes)
        {
            <li><a href="@node.Url">@node.Title</a> </li>
        }
    </ul>
}

In short, we can easily customize the TridionSitemapNode to use it as they way we want for simple or complex navigations.

Comments

Popular posts from this blog

Localization in Umbraco, Item Page field localization

Umbraco is one of the most deployed Web Content Management Systems on the Microsoft stack. It's in the top five most popular server applications and among the ten most popular open source tools in general. You can directly download and start using it from http://www.umbraco.com If you have not used the application yet, its always worth trying it once. I am sure you will love it. Well, for the first time users, Umbraco provide step by step instruction for initial set up, once you are done you can use some sample template or create a blank website. To know more on umbraco, Please refer below links http://our.umbraco.org/wiki/how-tos/a-complete-newbie's-guide-to-umbraco http://umbraco.com/help-and-support/video-tutorials/getting-started?freeVideos=1 http://our.umbraco.org/wiki/how-tos/getting-started-with-umbraco-what-is-next-after-you-install http://our.umbraco.org/wiki/install-and-setup ,  I have created a blank website, Now we will go step by step. Fi...

DD4TFormRouteHandler (posting a form as tridion page url)

ASP.NET routing enables us to use URLs that are not physical files, In DD4T we have the default page route definition. In which all page request redirct to Page controller and process the page. DD4TFormRouteHandler is a custom route handler responsible for mapping incoming browser requests to particular MVC controller actions, this works along with  DD4TFormHelper  (that generate the route information for the form) Posting a form in DD4T is not complicated, you can create the mvc form as a normal controller and action, then post it via AJAX. But, when we need to do post the form as normal page, It would need a tweak as the controller/action is not a page existed in tridion. This can be achieved by implementing a custom Mvc RoutHandler and reroute the posted form to the encrypted action and controller. It works as below daigram. So, how to do this. to render out the form we have BeginDD4TForm html helper as below that generate the form with encrypted route values....

Why not to have a static const in c#

This is just a thought, that I was thinking why can't we have a constant with static in C#, and the answer is 'NO'; That we cannot have a static constant; e.g: I created a class as below: public class Constants1 { public const string Const1 = "Hello"; public const string Const2 = "World"; public static string Static1 = "Hello Static"; } When we compile the program into IL, the C# compiler does a magic in IL, that the constants converts into static literals, of course it has to, that's why we are able to access the constants as Constants1.Const1