Cover image for Simple Navigation Macro for Umbraco

Simple Navigation Macro for Umbraco

adamkdean profile image Adam K Dean ・1 min read

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.

        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>

No nesting support but easily addable, see here.


Editor guide