Copied to clipboard

Flag this post as spam?

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


  • Jens Kvist 10 posts 30 karma points
    Mar 03, 2014 @ 17:42
    Jens Kvist
    0

    Highlight homepage when in root

    Hello guys!

    I have this razor script for my navigation:

    @inherits umbraco.MacroEngines.DynamicNodeContext


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

    <ul class="navigation_items" id="Navigation">
        <li class="What to write here?">
            <a href="/">Home</a>
        </li>  
        @foreach (var page in root.Children.Where("Visible"))
        {
            <li class="@page.IsAncestorOrSelf(Model, "current_item", "")">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>

    How do I make the "Home" list item the class "current_item" when I am in the root?

    Thank you! ^^

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 03, 2014 @ 18:44
    Fuji Kusaka
    0

    Try something like this

    string state = Model.NodeTypeAlias == "DocTypeA"? "class=current_item":"";
    <li @state">
            <a href="/">Home</a>
        </li>  
  • Jens Kvist 10 posts 30 karma points
    Mar 03, 2014 @ 19:04
    Jens Kvist
    0

    Hmm, I cannot figure it out...

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 03, 2014 @ 20:23
    Dennis Aaen
    0

    Hi Jens,

    Something like this should do it for you.

    @inherits umbraco.MacroEngines.DynamicNodeContext

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

    <ul class="navigation_items" id="Navigation">
      
      @{ var homeActive = ""; }

        @if(root.Id == Model.Id){
            homeActive = "current_item";
        }
       <li class="@homeActive>
            <a href="@root.Url">@root.Name</a>
        </li> 
        @foreach (var page in root.Children.Where("Visible"))
        {
            <li class="@page.IsAncestorOrSelf(Model, "current_item", "")">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>

    And you donĀ“t need to hardcode the URL and name of the frontpage item. You can use the @root.Url to get the URL and @root.Name to get the name.

    /Dennis

  • Jens Kvist 10 posts 30 karma points
    Mar 04, 2014 @ 09:01
    Jens Kvist
    0

    Thank you so much!

Please Sign in or register to post replies

Write your reply to:

Draft