Example Code to help consume Global Manifesto
Create a Service that can be injected into your code and razor templates that helps to understand Global Manifesto concepts like HomePage(), Website(), Breadcrumb()
public interface IPresentationService
{
ModelsBuilder.Site Site { get; }
ModelsBuilder.PageStandard Home { get; }
IEnumerable<Tuple<string, string>> Breadcrumb(IPublishedContent content);
}
public class PresentationService : IPresentationService
{
private const string SiteCacheKey = "104d0d13-6f5a-4d25-8266-e1ab15dcfbd4";
private const string HomeKeyCacheKey = "fe5992d2-7601-4c9a-b59d-debad90cb016";
private const string HomeCacheKey = "d5c3793e-6f16-4353-a4e0-d24d6a72a805";
private readonly IHttpContextAccessor httpContextAccessor;
private readonly IUmbracoContextFactory umbracoContextFactory;
private readonly IPublishedValueFallback publishedValueFallback;
public PortfolioService(IHttpContextAccessor _httpContextAccessor, IUmbracoContextFactory _umbracoContextFactory, IPublishedValueFallback _publishedValueFallback)
{
httpContextAccessor = _httpContextAccessor;
umbracoContextFactory = _umbracoContextFactory;
publishedValueFallback = _publishedValueFallback;
}
/* Returns current WebSite for current web request */
public ModelsBuilder.Site Site
{
get
{
var site = httpContextAccessor.HttpContext.Items[SiteCacheKey] as ModelsBuilder.Site;
if (site == null)
{
using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
{
site = new ModelsBuilder.Site(umbracoContextReference.UmbracoContext.PublishedRequest.PublishedContent.Ancestor(ModelsBuilder.Site.ModelTypeAlias), publishedValueFallback);
httpContextAccessor.HttpContext.Items[SiteCacheKey] = site;
}
}
return site;
}
}
/* Returns the current HomePage Id for current web request */
private Guid? HomePageKey
{
get
{
var home = httpContextAccessor.HttpContext.Items[HomeKeyCacheKey] as Guid?;
if (home == null)
{
home = Site.GetProperty(Umbraco.Cms.Core.Constants.Conventions.Content.InternalRedirectId)?.Value<GuidUdi>(publishedValueFallback)?.Guid;
httpContextAccessor.HttpContext.Items[HomeKeyCacheKey] = home;
}
return home;
}
}
/* Returns the current HomePage for current web request */
public ModelsBuilder.PageStandard HomePage
{
get
{
var home = httpContextAccessor.HttpContext.Items[HomeCacheKey] as ModelsBuilder.PageStandard;
if (home == null)
{
using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
{
var home = ModelsBuilder.PageStandard(umbracoContextReference.UmbracoContext.Content.GetById(HomePageKey));
httpContextAccessor.HttpContext.Items[HomeCacheKey] = home;
}
}
return home;
}
}
/* Returns the Breadcrumb in format (Item1 = Page Name, Item2 = Page Url) for current web request */
public IEnumerable<Tuple<string, string>> Breadcrumb(IPublishedContent content)
{
var breadcrumb = new List<Tuple<string, string>>();
var first = true;
while (content != null && content.Key != Site.Key)
{
var seo = (IPageSeo) content;
breadcrumb.Add(new Tuple<string, string>(string.IsNullOrWhiteSpace(seo.SeoTitle) ? content.Name : seo.SeoTitle, first ? null : content.Url()));
first = false;
content = content.Parent;
}
return breadcrumb.AsEnumerable().Reverse();
}
}
/* Standard Umbraco Code found at \Views\_ViewImports.html */
@using Umbraco.Extensions
@using Umbraco.Cms.Web.Common.PublishedModels
@using Umbraco.Cms.Web.Common.Views
@using Umbraco.Cms.Core.Models.PublishedContent
@using Microsoft.AspNetCore.Html
@using Stubborn.Infrastructure.Presentation.Exhibit
@using Stubborn.Infrastructure.Constitution
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Smidge
@inject Smidge.SmidgeHelper SmidgeHelper
/* Custom Code */
@inject YourNamespace.IPresentationService Presentation
@* Example breadcrumb implementation *@
<ol>
@foreach (var breadcrumb in Presentation.Breadcrumb(Model))
{
<li>
@if (breadcrumb.Item2 != null)
{
<a href="@breadcrumb.Item2">@breadcrumb.Item1</a>
}
else
{
<span>@breadcrumb.Item1</span>
}
</li>
}
</ol>