TagHelper

The UmbNav TagHelper provides a clean, declarative syntax for rendering menu items in Razor views. It handles link generation, active states, and HTML attribute output automatically.

Basic Usage

@using Umbraco.Community.UmbNav.Core.Models
@using Umbraco.Community.UmbNav.Core.TagHelpers

@{
    var menuItems = Model.Value<IEnumerable<UmbNavItem>>("mainNavigation");
}

<ul>
    @foreach (var item in menuItems)
    {
        <li>
            <umbnavitem menu-item="@item"></umbnavitem>
        </li>
    }
</ul>

Output

The TagHelper renders either an <a> tag for links or a configurable tag for labels:

For link items:

For text/label items:

Attributes Reference

Required Attributes

Attribute
Type
Description

menu-item

UmbNavItem

The menu item to render

URL Generation

Attribute
Type
Default
Description

mode

UrlMode

Default

URL generation mode: Default, Absolute, Relative

culture

string

null

Culture code for multi-language URL generation

Active State

Attribute
Type
Default
Description

active-class

string

null

CSS class to add when item is active

current-page

IPublishedContent

null

The current page for active state detection

is-active-ancestor-check

bool

false

Also mark ancestors of active page as active

Label Items

Attribute
Type
Default
Description

label-tag-name

string

"span"

HTML tag to use for text/label items

Detailed Examples

Basic Navigation

With Active State

Highlight the current page and its ancestors:

Multi-Level Navigation

Render nested menus with recursion:

Multilingual Sites

Specify culture for correct URL generation:

Custom Label Tags

Use a different tag for text items:

Absolute URLs

For external sharing or emails:

Understanding Active State

The TagHelper supports two methods for determining active state:

1. Pre-computed Active State

The IUmbNavMenuBuilderService automatically sets the IsActive property on items when building the menu:

2. Runtime Active Check

For more control, pass the current page to check at render time:

The is-active-ancestor-check option also marks parent items as active when a child page is the current page. This is essential for multi-level navigation where you want to show the path to the current page.

Handling Item Types

The TagHelper automatically handles different item types:

Renders as <a> with href, target, and rel attributes:

Content Items (UmbNavItemType.Document)

Renders as <a> with URL resolved from the content node:

Text Items (UmbNavItemType.Title)

Renders as the configured label tag (default: <span>):

Complete Example

A fully-featured navigation component:

Accessibility Considerations

When building navigation, consider these accessibility best practices:

CSS Output

The TagHelper preserves custom classes from menu items:

Performance Tips

  1. Cache menu items at the view level if your navigation doesn't change per-request

  2. Use the Menu Builder Service to filter items before rendering rather than in the view

  3. Limit depth in the Data Type to prevent deeply nested loops

Last updated

Was this helpful?