-
Notifications
You must be signed in to change notification settings - Fork 151
Custom Entity Pages
A Custom Entity Page is a special Page Type that dynamically serves a page for every custom entity using a url routing rule like '{id}/{urlSlug}'.
This is useful if you have a custom entity like blog posts or products where you want the advantages of having a dedicated custom entity type, but also have the advantages of the page templating and content block system.
The page template can have special sections defined that allow content blocks to be inserted for each of the dynamically generated pages. Let's illustrate this with an example:
Firstly we need to create a display model that will be used in the template.
using Cofoundry.Domain;
using Cofoundry.Web;
public class BlogPostDisplayModel : ICustomEntityPageDisplayModel<BlogPostDataModel>
{
public string PageTitle { get; set; }
public PageMetaData MetaData { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public string Author { get; set; }
}
A mapper class is also required to tell Cofoundry how to transform the raw custom entity data to this display model. The mapper supports constructor injection so this also give you the opportunity to include any extra data you might need in the view.
public class BlogPostDisplayModelMapper
: ICustomEntityDisplayModelMapper<BlogPostDataModel, BlogPostDisplayModel>
{
public Task<BlogPostDisplayModel> MapDisplayModelAsync(
CustomEntityRenderDetails renderDetails,
BlogPostDataModel dataModel,
PublishStatusQuery publishStatusQuery
)
{
var displayModel = new BlogPostDisplayModel();
displayModel.PageTitle = renderDetails.Title;
displayModel.MetaDescription = dataModel.ShortDescription;
displayModel.Title = renderDetails.Title;
displayModel.Date = renderDetails.CreateDate;
displayModel.Author = dataModel.Author;
return Task.FromResult(displayModel);
}
}
For the view we use an enhanced version of a Page Template, which should have a model type of ICustomEntityPageViewModel<TDisplayModel>
. As with regular PageTemplates you'll need to inject ICofoundryTemplatePageHelper<TModel>
in order to give access to the Cofoundry template helper and a strongly typed view model.
@using Cofoundry.Domain
@using Cofoundry.Web
@model ICustomEntityPageViewModel<BlogPostDisplayModel>
@inject ICofoundryTemplateHelper<ICustomEntityPageViewModel<BlogPostDisplayModel>> Cofoundry
@{
Cofoundry.Template.UseDescription("Template for the blog post details page");
var post = Model.CustomEntity.Model;
}
<div class="blog-content">
<h1>@post.Title</h1>
<h2>@post.Date by @post.Author</h2>
<div class="blog-lead">
@(await Cofoundry.Template.CustomEntityRegion("Lead")
.AllowBlockType<RichTextDataModel>()
.EmptyContentMinHeight(500)
.InvokeAsync())
</div>
<div class="blog-body">
@(await Cofoundry.Template.CustomEntityRegion("Body")
.AllowMultipleBlocks()
.AllowBlockType<RawHtmlDataModel>()
.EmptyContentMinHeight(500)
.InvokeAsync())
</div>
</div>
The template will be automatically registered with Cofoundry when the application is published or restarted. The page can then be added to the website in the Pages section of the admin panel as you would a regular page, except you will now be able to change the page type to be Custom Entity Page.