HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Render content

Introduces the concept for content rendering in Optimizely Content Management System (CMS).

Using MVC, the rendering is based on controllers and views, 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.

📘

Note

The examples here are based on MVC.

You have many options to control the rendering of any type of content in Optimizely Content Management System (CMS). 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 – Attribute used for adding meta data in a template. 
  • Tags and TemplateResolver – Used for selecting the template to apply in the current context.
  • Display channels – Used for previewing and rendering content in different devices and resolutions.
  • Display options – Used for controlling the layout of content, for example, 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 the page is added to a content area. The CMS content model and templates are explained in Page types and templates and Block types and templates.

Register and select 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 create your content types from the CMS Visual Studio extensions, or if you use a CMS base class for your template, then you do not need to explicitly implement the interface since this is done by the base class. Base classes are for example PageBase<T>, ContentControlBase<T>, BlockControlBase<T>, PageController<T>, PartialContentController<T> or BlockController<T>.

IViewTemplateModelRegistrator

CMS uses TemplateDescriptor or BasePageController<T>, where T is IContent, to resolve controllers. Partial views following the standard ASP.NET MVC conventions are automatically registered. 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 illustrated in the example below. This interface registers your template models to the template model collection.

Example: Assume we have this block type inheriting from a CMS 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. 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 MyEpiserverSite.Models.Blocks;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MyEpiserverSite.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 which 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 allow you to 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.