Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.
This topic introduces the concept for content rendering in Optimizely. Using MVC, rendering is based on controllers and views or Razor pages, which can be associated with templates for displaying content in certain context; for example, inside other content such as a page, or through different devices and channels.
A template in CMS is a component that represents something that can render a piece of content. A template can reference for example a MVC controller, a Razor page or a custom routing endpoint.
Note: The examples are based on .NET 5. See previous versions of this topic for ASP.NET MVC or Web Forms-based examples.
How it works
You have many options to control the rendering of any type of content in Optimizely. Using templates, you can apply multiple templates for any type of content, and have the system decide when to use which template.
These are the main components when working with templates:
- TemplateDescriptor. Adds metadata in a template.
- Tags and TemplateResolver. Selects the template to apply in the current context.
- Display channels. Previews and renders content in different devices and resolutions.
- Display options. Controls the layout of content, such as in a content area.
A content type can have several templates; for example, one for a web channel, and one for a mobile channel. A page can have a partial template to be used when you add the page to a content area. The Optimizely content model and templates are explained in Page types and templates and Block types and templates.
Registering and selecting templates
Templates must be registered to be evaluated for usage by the system. Registration is automatic if the template implements EPiServer.Web.IRenderTemplate<T>, where T states which model it can render. If you base your template on a base class in CMS then you do not need to explicitly implement the interface since this is done by the base class. Base classes are PageController<T>, PartialContentController<T>, PartialContentComponent<T>, AsyncPartialContentComponent<T>, BlockComponent<T> or AsyncBlockComponent<T>.
IViewTemplateModelRegistrator
Optimizely uses TemplateDescriptor and IRenderTemplate<T>, where T is IContentData, to resolve templates. Partial views following the standard ASP.NET MVC conventions are automatically registered (given that TemplateOptions.ScanViewsForTemplateRegistration is enabled). However, if you have partial views without controllers, you cannot use the TemplateDescriptor to register multiple templates against the content type. Instead you can use EPiServer.Web.Mvc.IViewTemplateModelRegistrator, as shown in the example below. This interface registers your template models to the template model collection.
Example: Assume we have this block type inheriting from an Optimizely base class.
[ContentType]
public class TeaserBlock : BlockData
{
public virtual string Heading { get; set; }
public virtual XhtmlString MainIntro { get; set; }
}
If there is a partial view in /View/Shared/TeaserBlock.cshtml that has a model set to TeaserBlock, that partial view is automatically registered (given that TemplateOptions.ScanViewsForTemplateRegistration is enabled). To register multiple templates for the partial view, add a class, for example, in the Business folder of your project, inheriting from IViewTemplateModelRegistrator:
using EPiServer.Framework.Web;
using EPiServer.Web.Mvc;
using MyOptimizelySite.Models.Blocks;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyOptimizelySite.Business
{
public class ViewTemplateModelRegistrator : IViewTemplateModelRegistrator
{
public void Register(TemplateModelCollection viewTemplateModelRegistrator)
{
viewTemplateModelRegistrator.Add(typeof(TeaserBlock),
new EPiServer.DataAbstraction.TemplateModel()
{
Name = "SidebarTeaser",
Description = "Displays a teaser of a page.",
Path = "~/Views/Shared/SidebarTeaserBlock.cshtml",
Tags = new string[]{RenderingTags.Sidebar}
}
)};
}
}
Template selection
A page or a block can have multiple associated templates, and pages can have partial templates that are used when the page is added to a content area. The final template selected for rendering a content instance depends on the specific context. The TemplateResolver selects the most appropriate template for rendering in the current context based on the information in the TemplateDescriptor attribute, any defined display channels, and tags applied to a template. See Selecting templates.
Display channels and display options
Based on templates, these features let you control how content should render using different devices, and how content should be laid out. See Display channels and Display options.
Navigation menus
See Listings and navigation for examples how to build navigation menus using different methods.
Related topics
Do you find this information helpful? Please log in to provide feedback.
Last updated: Jul 02, 2021