Loading...
Area: Optimizely CMS
Applies to versions: 12 and higher
Other versions:

Changing display channel programmatically

Recommended reading 

This topic describes how to programmatically change the template that is used to render a page.

Example

Given that there is a page model registered like:

[ContentType]
public class TypedPage : PageData
{
  public virtual string Heading { get; set; }
  public virtual XhtmlString Content { get; set; }
}

You can have several templates defined for the same typed page. The following example registers two templates for the same typed page.

  • One is StandardTemplate without tags which will be the default template to use when rendering page instances of type TypedPage.
  • The other template is another generic template for all pages that is more suitable for rendering for mobile devices.
[TemplateDescriptor]
public partial class StandardTemplate : PageController<TypedPage>
{ }

[TemplateDescriptor(Name = "MobileTemplate")]
public partial class MobileTemplate : PageController<PageData>
{ }

To change the template that is used for a request, you can attach an event handler to an event that is raised when a template is chosen for a specific request. The following code sample attaches to the event and changes the template for request for TypedPage instances when the request comes from a mobile device.

[InitializableModule]
public class MobileRedirectSample : IInitializableModule
{
  private IHttpContextAccessor _httpContextAccessor;
  public void Initialize(InitializationEngine context)
  {
    _httpContextAccessor = context.Locate.Advanced.GetRequiredService<IHttpContextAccessor>();
    context.Locate.Advanced.GetRequiredService<ITemplateResolverEvents>().TemplateResolved
      += new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
  }

  public void Uninitialize(InitializationEngine context)
  {
    context.Locate.Advanced.GetRequiredService<ITemplateResolverEvents>().TemplateResolved
      -= new EventHandler<TemplateResolverEventArgs>MobileRedirectSample_TemplateResolved);
  }

  void MobileRedirectSample_TemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
  {
    if (eventArgs.ItemToRender != null && eventArgs.ItemToRender is TypedPage)
    {
      //The sample code uses package 'Wangkanai.Detection' for device detection
      var detection = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IDetection>();
      if (detection.Device.Type == DeviceType.Mobile)
      {
        var mobileRender = eventArgs.SupportedTemplates
          .SingleOrDefault(r => r.Name.Contains("Mobile") &&
            r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);

        if (mobileRender != null)
        {
          eventArgs.SelectedTemplate = mobileRender;
        }
      }    
    }
  }

  public void Preload(string[] parameters)
  {
  }
}
Do you find this information helpful? Please log in to provide feedback.

Last updated: Sep 28, 2021

Recommended reading