Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael 8 posts 28 karma points
    Jun 03, 2011 @ 17:18
    Michael
    0

    Navigation with selected item CSS class

    Hello,

    I am new to Umbraco, so I apologise if this is a bit of a newbie question.

    I am attempting to create a top level navigation for my site with a razor macro. I have got as far as my code below with the help of the documentation I could find online. It renders the nodes I require, but I am uncertain as to how I attach a CSS class to the current node, so that I can indicate to the user that is where they are within my site.

    If there is an easier way to do this with Razor, I'd be happy to hear. I'm not looking to go down the XSLT route if I can help it.

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{ 
        int rootId = 1084; // TODO: not hard code

        var root = @Model.NodeById(rootId);
        DynamicNodeList pages = root.Children;
        DynamicNode home = root;
           
        <ul>
            <li><a href="@home.Url">@home.NodeTypeAlias</a></li>

            @foreach (DynamicNode page in pages)
            {
               if (page.HasProperty("includeInMenu"))
               {
                   var includeInMenu = page.GetProperty("includeInMenu");

                   if (includeInMenu.Value == "1")
                   {
                       <li><a href="@page.Url">@page.Name</a></li>
                   }
               }
            }
        </ul>
    }

    Thanks,

    Michael

  • Richard Boelen 61 posts 153 karma points
    Jun 03, 2011 @ 17:39
    Richard Boelen
    0

    Hi ,

    if you go and create a new "Scripting File" in the developer section, you can choose to create a razor script based on a predefined script.

    Take a look at the "Navigation" script sample, it includes a level for your navigation, you can specify the css class you want to use for your UL and it supports a "selected" css class for your current item. Should be easy to tweak this to your needs.

    Cheers, Richard

  • Richard Boelen 61 posts 153 karma points
    Jun 03, 2011 @ 17:43
    Richard Boelen
    0

    Btw, if your document types use a property type TRUE/FALSE and alias umbracoNaviHide, than you can use @Model.Children.Where("Visible") to select only the items that have umbracoNaviHide set to true, so no need for the property checking on includeInMenu, unless you have different functional specs  :-)

  • Michael 8 posts 28 karma points
    Jun 03, 2011 @ 20:09
    Michael
    0

    I thought there might be an easier way! That's great, thanks for the help.

    Nothing was lost in my experimenting though, was good to see how Umbraco works.

Please Sign in or register to post replies

Write your reply to:

Draft