Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created May 2, 2023 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KevinJump/6027e5348ab3d3af33740fc5d7b4354e to your computer and use it in GitHub Desktop.
Save KevinJump/6027e5348ab3d3af33740fc5d7b4354e to your computer and use it in GitHub Desktop.
Display all the properties (and nested properties) for a content node in Umbraco 10/11.
@using Umbraco.Cms.Web.Common.PublishedModels;
@using Umbraco.Cms.Core.Models;
@using Umbraco.Cms.Core.Models.Blocks;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@{
Layout = null;
}
<html>
<head>
<title>@Model.Name - @Model.ContentType.Alias</title>
<style>
body { padding: 0; margin: 0;}
.publishedContent,
.publishedElement {
margin: 5px;
padding: 5px;
border: 1px solid red;
background-color: rgba(0,0,0,0.05);
}
.property {
background-color: rgba(0,0,0,0.05);
padding: 6px;
border: 1px dotted blue;
margin: 5px;
display: flex;
align-content: center;
align-items: center;
}
.property .hasValue {
padding: 5px;
}
.property .name {
font-weight: bold;
width: 250px;
}
.property .type {
font-style: italic;
width: 350px;
border-right: 1px dotted blue;
word-wrap: break-word;
}
.property .value {
word-wrap: break-word;
padding: 5px;
}
.blocklist {
border-left: 5px solid green;
}
.nestedContent {
border-left: 5px solid gold;
}
.breadcrumb {
background: #bbdefb;
padding: 5px;
}
.breadcrumb a {
display: inline-block;
padding: 5px;
}
.grid {
border: 1px dotted purple;
}
.grid .umb-grid * {
border: 1px dotted lightpink;
}
</style>
</head>
<body>
@{
RenderBreadCrumb(Model);
RenderContent(Model);
}
</body>
</html>
@{
void RenderBreadCrumb(IPublishedContent content)
{
<div class="breadcrumb">
@foreach (var p in content.AncestorsOrSelf().OrderBy(x => x.Level))
{
<span>📄</span> <a href="@p.Url()">@p.Name</a>
}
</div>
}
}
@{
void RenderType(object? value)
{
var type = value?.GetType() ?? null;
@type
}
void RenderContent(IPublishedElement content)
{
<div class="publishedContent">
@foreach (var property in content.Properties)
{
var value = content.Value(property.Alias);
var hasValue = content.HasValue(property.Alias);
<div class="property">
@{
var i = hasValue ? "✔️" : "❌";
<div class="hasValue">@i</div>
}
<div class="name">@property.Alias</div>
<div class="type">@{
RenderType(value);
}</div>
<div class="value">
@if (value == null)
{
<div>NULL</div>
}
else
{
@switch (value)
{
case string stringValue:
<div>@stringValue</div>
break;
case string[] stringArray:
<ul>
@foreach (var s in stringArray)
{
<li>@s</li>
}
</ul>
break;
case Link singleLink:
RenderLink(singleLink);
break;
case List<Link> links:
RenderLinks(links);
break;
case List<IPublishedContent> contentList:
RenderContentList(contentList);
break;
case List<IPublishedElement> contentElements:
RenderContentElements(contentElements);
break;
case IPublishedContent publishedContent:
RenderContentStub(publishedContent);
break;
case BlockListModel blockList:
RenderBlockList(blockList);
break;
case Newtonsoft.Json.Linq.JObject jObject:
RenderGrid(content, property.Alias);
break;
default:
<div>@value</div>
break;
}
}
</div>
</div>
}
</div>
}
void RenderContentList(List<IPublishedContent> contentList)
{
foreach (var contentItem in contentList)
{
RenderContentStub(contentItem);
}
}
void RenderContentStub(IPublishedContent content)
{
<div>@content.Name (@content.Id)</div>
}
void RenderContentElements(List<IPublishedElement> contentElements)
{
<div class="nestedContent">
@foreach (var element in contentElements)
{
RenderContent(element);
}
</div>
}
void RenderBlockList(BlockListModel blockList)
{
<div class="blocklist">
@foreach (var element in blockList)
{
RenderContent(element.Content);
}
</div>
}
void RenderLinks(List<Link> links)
{
<ul>
@foreach (var link in links)
{
<li>@{
RenderLink(link);
}</li>
}
</ul>
}
void RenderLink(Link link)
{
@link.Url @link.Name
}
void RenderGrid(IPublishedElement content, string alias)
{
<div class="grid">
@Html.GetGridHtml(content, alias)
</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment