Mapping multiple templates of control

February 23, 2009 Digital Experience
[This post is part of the developer's manual preview published on this blog. You can find temporary TOC here.]
 

[This article requires Sitefinity 3.6 hotfix to be installed]

 

In the previous article we have demonstrated how to map embedded template of a control to an external one. A careful reader may have noticed that we have set the layoutTemplatePath property of viewSettings element indicating that the control has only one template - the layout template - which we are mapping.

 

While, it is true that all Views (controls based on ViewModeControl and ViewModeUserControl) indeed have only one template and that most of other controls have only one template, there are several controls that sport multiple templates.

 

In this article, we will examine how to map multiple templates of such controls.

 

Before we start, let us quickly take a glance at the list of Controls that use more than one template:

  • Telerik.Blogs.WebControls.BlogPosts (based on Telerik.Cms.Engine.WebControls.ContentView)
  • Telerik.Cms.Engine.WebControls.ContentView
  • Telerik.Events.WebControls.EventsView (based on Telerik.Cms.Engine.WebControls.ContentView)
  • Telerik.Libraries.DownloadList
  • Telerik.Libraries.WebControls.ImageGallery
  • Telerik.Libraries.WebControls.ViddlerVideo
  • Telerik.Libraries.WebControls.Video
  • Telerik.Lists.WebControls.ListDisplay
  • Telerik.News.WebControls.NewsView (based on Telerik.Cms.Engine.WebControls.ContentView)

Another way to spot these controls is to open the reference Controls Config file that can be downloaded from your Client.net account and look for viewSettings elements which have a collection of additionalTemplates.

 

 

AdditionalTemplates collection



In order to map multiple templates of a control or View, you need to define the additionalTemplates collection inside of the viewSettings template. We know that for example BlogPosts control uses two templates: ItemListTemplate and SingleItemTemplate. First used for displaying a list of posts and second used for displaying one single post. To map both of these templates to external ones, we can place following xml in our Controls Config file, between the viewMap nodes:
<viewSettings hostType="Telerik.Blogs.WebControls.BlogPosts"
      <additionalTemplates> 
           <!--Provides user interface for displaying a list of posts in the BlogPosts control in the blogs module.--> 
           <add key="ItemListTemplatePath" layoutTemplatePath="~/Sitefinity/ControlTemplates/Blogs/ContentViewItemList.ascx" /> 
           <!--Provides user interface for displaying a single post in the BlogPosts control in the blogs module.--> 
           <add key="SingleItemTemplatePath" layoutTemplatePath="~/Sitefinity/ControlTemplates/Blogs/ContentViewSingleItem.ascx" /> 
      </additionalTemplates> 
    </viewSettings> 
 
 

 

Just as it was case with mapping controls with only one template, you will actually need to provide these templates at the paths you have specified.

 

*** IMPORTANT ***

 

Every time you modify ControlsConfig file it is necessary to restart the application in order for the changes to be applied. While there are numerous ways to restart an application, here are few handy ones - you can restart the IIS server, resave web.config file (e.g. open web.config file, press space, press backspace, save config.file), resave global.asax file (e.g. open global.asax file, press space, press backspace, save global.asax file)…

 

*** END IMPORTANT ***

 

*** NOTE ***

 

If you map to an external template that ends with the .ascx extension, you will be using standard ASP.NET template parser which means you will also need to provide localization file in the App_LocalResources file. Resources files are also part of the package that comes with ExternalTemplates.zip file.
If, on the other hand, you decide to use Sitefinity parser, embedded localization resources will be used. Sitefinity parser will be used if template ends in any other extension except .ascx (though the convention is .sft).

 

*** END NOTE

It is important to note that we are not required to map all the templates of a control to external ones. For example, we could map only the SingleItemTemplate of the BlogPosts control to an external template, while for ItemListTemplate we could keep using embedded one. To achieve this, our configuration would look like this:

<viewSettings hostType="Telerik.Blogs.WebControls.BlogPosts"
      <additionalTemplates> 
           <!--Provides user interface for displaying a single post in the BlogPosts control in the blogs module.--> 
           <add key="SingleItemTemplatePath" layoutTemplatePath="~/Sitefinity/ControlTemplates/Blogs/ContentViewSingleItem.ascx" /> 
      </additionalTemplates> 
    </viewSettings> 
 

 

Notice that we have only removed the mapping for the ItemListTemplatePath property.

 

*** NOTE ***

 

Experienced users of Sitefinity know that ContentView based templates, such as BlogPosts, EventsView and NewsView have ContentViewDesigners attached to them which can dynamically load templates. We will touch on the topic of designers and mapping their templates in one of the following articles in this topic.

 

*** END NOTE ***

The Progress Team