Files
Umbraco-CMS/src/Umbraco.Web.UI.NetCore/Umbraco/PartialViewMacros/Templates/SiteMap.cshtml
Bjarke Berg 1594a84d80 migated remaining backoffice controllers
Signed-off-by: Bjarke Berg <mail@bergmania.dk>
2020-06-22 10:08:08 +02:00

45 lines
1.3 KiB
Plaintext

@using Umbraco.Core
@using Umbraco.Core.Models.PublishedContent
@using Umbraco.Web
@inherits Umbraco.Web.Common.Macros.PartialViewMacroPage
@*
This snippet makes a list of links of all visible pages of the site, as nested unordered HTML lists.
How it works:
- It uses a custom Razor helper called Traverse() to select and display the markup and links.
*@
@{ var selection = Model.Content.Root(); }
<div class="sitemap">
@* Render the sitemap by passing the root node to the traverse helper, below *@
@Traverse(selection)
</div>
@* Helper method to traverse through all descendants *@
@helper Traverse(IPublishedContent node)
{
@* Update the level to reflect how deep you want the sitemap to go *@
const int maxLevelForSitemap = 4;
@* Select visible children *@
var selection = node.Children.Where(x => x.IsVisible() && x.Level <= maxLevelForSitemap).ToArray();
@* If any items are returned, render a list *@
if (selection.Length > 0)
{
<ul>
@foreach (var item in selection)
{
<li class="level-@item.Level">
<a href="@item.Url()">@item.Name</a>
@* Run the traverse helper again for any child pages *@
@Traverse(item)
</li>
}
</ul>
}
}