HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback


Display channels let you control which template to use in a specific rendering context; for example, when previewing and displaying content targeted for mobile devices with different resolutions.

Note

The examples are based on MVC.

`EPiServer.Web.DisplayChannel` is a base class that is used for creating display channels, to detect viewport settings, and select templates to render content for a targeted device format.

The display channel is evaluated for each rendering request. Using matching **tags**, you can control exactly which template to use in a specific context. [Selecting templates](🔗) prefers templates with a tag matching a `DisplayChannel`, and the output is scaled and resized to fit the targeted device. If no such template exists, it falls back to use the most suitable template.

No channels are available by default; you need to add them to your solution. You do not need to specifically register channels, as the system automatically scans and registers all discovered active channels during initialization.

Optimizely Content Management System (CMS) has built-in functionality to let editors preview the output in edit view, and applying available display channels and their resolutions, before publishing. Inheriting from `EPiServer.Web.IDisplayResolution` adds resolution preview options to the edit view.

## Create a display channel

In the following example, a `MobileDisplayChannel` display channel class is added to a _Channel_ folder in the (MVC) _Business_ folder of the solution in Visual Studio.

The class inherits from `DisplayChannel`, and the `IsActive` method adds logic controlling whether the channel can be considered active. When evaluating for display channel, the system checks which channels are active, and prefers templates with a tag included in `EPiServer.Framework.DataAnnotations.TemplateDescriptorAttribute.Tags`, and that matches `EPiServer.Web.DisplayChannel.ChannelName` for the active channel.

**Example:** A display channel active for mobile devices.



If you have two different templates registered for same content type, and one template has a tag matching the `ChannelName`, then that template is used when the channel is active.

**Example:** Using tags for rendering in a mobile device context.



## IDisplayMode

To register a `System.Web.WebPages.IDisplayMode`, you register your instance with `EPiServer.Web.DisplayChannelService`. The reason for registering `IDisplayMode` towards Optimizely instead of directly against ASP.NET, is so editors can preview the channel even if the request itself does not match the channel condition. This way editors can preview content as it would appear in a mobile device, when working in a regular web browser.

Otherwise the same conventions as for `System.Web.Mvc` apply; if a “mobile” channel is active then the convention is to prefer a view named like _Index.mobile.cshtml_ before one named _Index.cshtml_.

**Example:** Registering a simple `IDisplayMode` active for mobile devices.



## DisplayChannelService

You can get active channels from the `DisplayChannelService`, which is useful when you want to select stylesheet depending on active channel. You can retrieve the service from `EPiServer.ServiceLocation.ServiceLocator.Current` or `EPiServer.ServiceLocation.ServiceLocationHelper`, which is exposed in several locations, such as in `PageBase` and `InitializationEngine` if you have a using statement for `EPiServer.Web`.

**Example:** Setting a CSS class depending on whether a mobile channel is active.



## Add preview resolutions and devices

You can change the size of the preview view port to let editors visualize content as it would appear in different resolutions. CMS also has a built-in feature for adding background device images for visualization, see below.

In the following example, a mobile display channel is created and associated it with a specific resolution of (320x658). The channel and resolution are associated by returning the FullName of the `MobileResolution` type.

**Example:** Display channel for mobile display.



**Example:** Resolution class for mobile display.



Combining these adds the Mobile preview option in edit view, with the Mobile (320x568) as preselected resolution.



## Add a background device image

In the following example, a preview option is added for the Google Nexus 7 tablet to the preview resolutions drop-down list.

If you inspect the HTML structure around the preview iframe you will notice an auto-generated CSS class named **epi-viewPort-600x790**, where the numbers correspond to the dimensions specified in the definition.

Define this CSS class to customize the look of the view port – assuming you are in the CMS sample project – you add a new CSS file _ClientResources/Style/displayresolutions.css_ with the following contents:



The image _nexus7.png_ referenced in the CSS class above is a background image picturing the Nexus 7, and is shown behind the scaled down view port.

To get this CSS file loaded when the user interface loads, piggy-back on the CMS module’s resource bundle. Knowing that the CMS module depends on a _epi.cms.widgets.base_ resource bundle, add your CSS file to this resource bundle from the _module.config_ of your project.



The _displayresolutions.css_ file is loaded whenever the CMS module is started; in other words, when going to the editorial interface.

Note

There is no file watch on _module.config_, so changes are not picked up automatically by the application. Therefore you have to restart the application by other means to see the changes on the site.

Select Nexus 7 in the resolution drop-down and get the preview as shown below, with the browser preview to the left and the actual rendering to the right for comparison. While this approach does not give the exact same result as on the actual device, it can give the editor a very close idea of how a page will look on a different screen.