Copied to clipboard

Flag this post as spam?

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


  • Mikkel 14 posts 115 karma points
    Apr 28, 2017 @ 08:24
    Mikkel
    1

    if currentpage is frontpage add current class

    Hey :)

    I've been trying to make a navigation menu for the first time.

    I've most of it working, but can't figure out how to make the frontpage get the "current" class when that page is active.

    Here is what i've for now:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    
    @{ var selection = CurrentPage.Site().Children.Where("Visible"); }
    
    <ul>
        <li><a href="/">Home</a></li>
        @foreach (var item in selection)
        {
            <li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
                <a href="@item.Url">@item.Name</a>
                @if (item.Children.Where("Visible").Count() > 0) {
                    <ul class="dropdown">
                        @foreach (var subpage in item.Children.Where("Visible")) {
                            <li><a href="@subpage.Url">@subpage.Name</a></li>
                        }
                    </ul>
                }
            </li>
        }
    </ul>
    

    Thanks in advance :)

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 28, 2017 @ 09:11
    Dave Woestenborghs
    0

    Hi Mikkel,

    What do you mean with front page ? Your homepage ?

    Dave

  • Mikkel 14 posts 115 karma points
    Apr 28, 2017 @ 09:13
    Mikkel
    0

    Hey Dave :)

    Yes that's what i mean

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 28, 2017 @ 09:26
    Dave Woestenborghs
    101

    Hi Mikkel,

    This example should work. You only need to replace "HomePageDocTypeAlias" with the actual alias of your homepage doctype :

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var selection = Model.Content.AncestorOrSelf(1).Children().Where(x => x.IsVisible()).ToList();
    }
    
    <ul>
        <li class="@(Model.Content.DocumentTypeAlias == "HomePageDocTypeAlias" ? "current" : null)"><a href="/">Home</a></li>
        @foreach (var item in selection)
        {
            <li class="@(item.IsAncestorOrSelf(Model.Content) ? "current" : null)">
                <a href="@item.Url">@item.Name</a>
    
                @{
                    var children = item.Children.Where(x => x.IsVisible()).ToList();
                }
                @if (children.Any())
                {
                    <ul class="dropdown">
                        @foreach (var subpage in children)
                        {
                            <li><a href="@subpage.Url">@subpage.Name</a></li>
                        }
                    </ul>
                }
            </li>
                    }
    </ul>
    

    I also took the liberty to refactor it to use strongly typed content (Model.Content) access instead of the use of dynamics (CurrentPage)

    To have a good understanding between the difference read this article I wrote : http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

    Dave

  • Mikkel 14 posts 115 karma points
    Apr 28, 2017 @ 09:33
    Mikkel
    0

    Hey again :D

    Thank you! Works perfectly, going to dig into your article asap :)

Please Sign in or register to post replies

Write your reply to:

Draft