DEV Community

Cover image for Simple Navigation Macro for Umbraco
Adam K Dean
Adam K Dean

Posted on

Simple Navigation Macro for Umbraco

I have started working with Umbraco, so a lot of my posts are probably going to be about that for the next few months at least.

This snippet is probably common knowledge for a lot of Umbraco developers, but for me, I had to look on the internet for almost 30 seconds to find it, so to make it easier for me to find it in the future, I'm posting it here. You're welcome future me.

<ul>
    @{ 
        var homeNode = Model.AncestorOrSelf("Home"); 
        var homeClass = Library.If(homeNode.Id == Model.Id, "selected", "");
    }

    <li><a href="@homeNode.Url" class="@homeClass">@homeNode.Name</a></li>      

    @foreach(var pageNode in homeNode.Children.Where("Visible"))
    {
        var pageClass = Library.If(pageNode.Id == Model.Id, "selected", "");

        <li><a href="@pageNode.Url" class="@pageClass">@pageNode.Name</a></li>
    }
</ul>
Enter fullscreen mode Exit fullscreen mode

No nesting support but easily addable, see here.

Discussion (0)