November Happy Hour will be moved to Thursday December 5th.

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

This topic introduces the concept for content rendering in Episerver. 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. Refer to previous versions of this topic for Web Form-based examples.

How it works

You have many options to control the rendering of any type of content in Episerver. 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 for a template. 
  • Tags and TemplateResolver - used for selecting the template to apply for the currrent 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 Episerver content model and templates are explained in Page types and templates and Block types and templates.

Registering and selecting templates

Templates need to 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 created your content types from the Episerver Visual Studio extensions, or if you use an Episerver 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

Episerver uses TemplateDescriptor or BasePageController<T> where T is IContent, to resolve controllers. Partial views that follow 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 will register your template models to the template model collection.

Example: Assume we have this block type inheriting from an Episerver 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 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. Based on the information in the TemplateDescriptor attribute, any defined display channels, and tags applied to a template, the TemplateResolver will select the most appropriate template for rendering in the current context. 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.

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Sep 21, 2015

Recommended reading