Skip to content

Instantly share code, notes, and snippets.

@markadrake
Last active April 11, 2022 22:53
Show Gist options
  • Save markadrake/af8bf03d5d61e296883447bd5d4b9b68 to your computer and use it in GitHub Desktop.
Save markadrake/af8bf03d5d61e296883447bd5d4b9b68 to your computer and use it in GitHub Desktop.
Umbraco 9 | Sitemap in Razor
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@{
Layout = "template.cshtml";
var currentPage = Umbraco.AssignedContentItem;
var root = currentPage.AncestorOrSelf(1);
}
<div class="sitemap">
<ul>
@foreach(var page in root.Children()) {
listChildren(page);
}
</ul>
</div>
@functions {
public void listChildren(IPublishedContent page) {
var children = page.Children();
<li>
<a href="@(page.Url())">@(page.Name)</a>
@if(children.Any()) {
<ul>
@foreach(var child in children) {
listChildren(child);
}
</ul>
}
</li>
}
}
@markadrake
Copy link
Author

Does the tab spacing look like shit? That's because GitHub defaults to 8 spaces! You can change this default by visiting the appearance section of your profile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment