Copied to clipboard

Flag this post as spam?

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


  • Bridgette 3 posts 24 karma points
    Jul 14, 2014 @ 22:53
    Bridgette
    0

    Creating subnavigation

    Hi there!  I am a front-end designer new to Umbraco and Razor. I'm working on a vertical subnavigation. I have an "active" class working for third-level navigation, but it's applying to the entire list. How do I ensure the active class works only for the active page, and not for other navigation items in the list?  Apologies for the simpleton question, I just don't know how to write the syntax! 

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{ 
        @*Get the root of the website *@
        var root = Model.AncestorOrSelf(2);
    }
    
    <ul class="nav nav-pills nav-stacked subnav">
        @foreach (var page in root.Children.Where("Visible"))
        { 
            <li class="@page.IsAncestorOrSelf(Model, "active", "")">
                <a href="@page.Url">@page.Name</a>
                    @if (page.Childen != null && page.Children.Count() > 0)
                {
                    <ul class="subnav">
                    @foreach (dynamic subpage in page.Children.Where("Visible")){
                        <li class="@subpage.IsAncestorOrSelf(Model, "active", "")">
                             <a href="@subpage.Url">@subpage.Name</a>
                        </li>
                    }
                    </ul>
                }
            </li>
        }
    </ul>

    This produces this result:

    But what I'm really trying to achieve is this:

    thanks!
    bridgette

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 15, 2014 @ 00:10
    Dennis Aaen
    0

    Hi Bridgette and welcome to our,

    I thinking the reason why all all your li´s are getting the background color is because that you´re using the same class in both for each loops, you cold try to change the first one to current e.g 

    Try this, as you can see I have change the class at the first foreach to current.

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
        @*Get the root of the website *@
        var root = Model.AncestorOrSelf(2);
    }

    <ul class="nav nav-pills nav-stacked subnav">
        @foreach (var page in root.Children.Where("Visible"))
        {
            <li class="@page.IsAncestorOrSelf(Model, "current", "")">
                <a href="@page.Url">@page.Name</a>
                    @if (page.Childen != null && page.Children.Count() > 0)
                {
                    <ul class="subnav">
                    @foreach (dynamic subpage in page.Children.Where("Visible")){
                        <li class="@subpage.IsAncestorOrSelf(Model, "active", "")">
                             <a href="@subpage.Url">@subpage.Name</a>
                        </li>
                    }
                    </ul>
                }
            </li>
        }
    </ul>
    I think that you should use Partial view marcos instead of dynamic node razor. The DynamicNode Razoris a old legacy Razor and will be deprecated.

    And the rewriting from DynamicNode Razor to Dynamic Razor or Strongly Typed Razor isn´t so hard. There are not so big difference. E.g instead of @Model you´re using the @CurrentPage. for the dynamic razor and Model.Content fro the strongly typed razor. You can see the differencehere: http://our.umbraco.org/documentation/reference/templating/macros/Partial-View-Macros/

    Here are the documententation for getting data out forbuilt-in-property-editors, the both version 6 and 7.

    http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors/

    http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/

    Other good resources could be the cheat sheets for both types of razor, dynamic razor and strongly typed razor,

    This one is the cheat sheet for dynamic razor v6Razorcheatsheet.pdf and this one is for the strongly typed razor. v6strongcheatsheet.pdf

    Hope this helps,

    /Dennis

  • Bridgette 2 posts 22 karma points
    Jul 15, 2014 @ 00:34
    Bridgette
    0

    Many thanks, Dennis!

     

Please Sign in or register to post replies

Write your reply to:

Draft