Tuesday, June 10, 2008

Multiple Templates for a PageType

It looks like a Dev / Partner summit is the perfect birthplace for ideas to grow. Here is yet another idea that grew out of discussions with partners, eMVPs and other Guru's at the summit: It could be cool to have multiple renderings (templates) for a page type. Today several PageType's can share the same template, but it's not possible to specify more than one template to use for a given PageType - a feature which would be very handy when for instance you wanted to make a special set of templates to show your site to mobile users. Or perhaps just special templates for IE or Firefox. Or maybe just a "Printable" set of templates. In any case I think this can be an important building block in making flexible websites.

So, naturally the day after the summit (a bit hungover I must admit) I sat down and began coding. There's several ways to achieve this goal - VPP's being one of them, but to keep it simple I decided to just hook into the UrlRewriting and change the internal url for the pages before they were executed. I ended up with this simple prototype that can change the executing template for a page depending on the User Agent of the client. I'll supply both the source and the compiled versions below - but keep in mind that it's just a prototype and it IS PROVIDED AS IS.

In order to use it, place the assembly in your bin, and add the following to the section-registration of your web.config:

<section name="multiRenderings" type="EPiServer.Research.MultiRenderingSection, MultiRenderingPlugin" allowLocation="true"/>

Then, After the "episerver" section you can add a list of the renderings:

<multiRenderings>
  <renderings>
    <rendering name="IE" userAgent=".*MSIE.*"/>
  </renderings>
</multiRenderings>

This rendering above will try to execute the IE template for userAgents matching the regular expression: ".*MSIE.*" - meaning that all IE browsers will be sent to templates located in the "IE" subfolder (if such templates exist).

On my test site I have the following folder structure:

/      
  Templates    
    MyPage.aspx  
    MyPage2.aspx  
    IE  
      MyPage.aspx

 

When looking at a page that uses "MyPage.aspx" IE will be using the "MyPage.aspx" located in the "IE" folder, but since MyPage2.aspx doesn't exist there, MyPage2.aspx will fall back to the one in "Templates".

This will probably become a lot more elegant as the plugin evolves - but I think it already now might proof useful to some of you, which is why I post it here.

Download from labs.

4 comments:

Anonymous said...

hi allan
i know my comment doesn't directly relate to your geek post, but i so liked the words "episerver" and "plugin" and "template" in the same one post, and you have always given me hope that anything is possible, (and that its not hard at all) so here goes: Do you know of any plugin-like software that enable handling and distribution of electronic postcards in episerver (i mean so users can send postcards to each other)? Or of any other "viral quality" widgets or plugins that can be used (quick and easy) with epi? Thank you in advance, my friend! :-)

Allan Thræn said...

Nanna, I can't think of any of the top of my head, but let me dig a bit and see what I come up with. I'll do anything for my favorite customer :-)

Anonymous said...

Awesome post. I will use it in the future. I will try to use it for labs and for world

Ted Nyberg said...

Hey Allan, I remember we were discussing this at the big fancy dinner following the Partner Summit! I'll definitely try your approach! Cheers!