Try our conversational search powered by Generative AI!

Deane Barker
Dec 8, 2010
  6136
(2 votes)

Selectively Overriding Page Type Template Mapping

One thing which has always been a mild frustration to me is EPiServer’s template mapping.  The concept of each Page Type only having one template always seemed restrictive.  The concept works fine in 95% of cases, but the other 5% can be a pain.  What if you have one page you want to do something a little (or a lot) differently than other pages of the same Page Type?

In these cases, the obvious solution is to create a new Page Type and specify a different template for it.  But I hated the idea of creating a new Page Type just to get a new ASPX file to render.  You have to copy all the properties over to the new type, and you now have to keep the two Page Types in sync.  With one extra type, it’s not so bad.  When you get a dozen or so, it becomes a problem.

Furthermore, right now we have a client coming off a Serena Collage install, managed in Dreamweaver.  (For those not familiar, Collage was really an SCM system, re-purposed as a CMS.  You didn’t manage “content,” as much as you managed raw HTML files.  Think Subversion, but with some CMS-ish functionality wrapped around it.  Collage was discontinued and went unsupported two years ago, and I don’t think we’ll ever see another CMS quite like it.)

Consequent to this experience, the client’s Web guy is very hands-on, and has created many landing pages which are painstakingly hand-crafted HTML.  To make their transition to EPiServer less painful, we needed to give him the ability to “escape” from type-bound templates whenever he felt the need, and be able to hand-write HTML in his editor of choice.

So, I set out to find a way to be able to specify the template for a particular page.  I originally started down the road of creating a bizarre contraption of dynamically-loaded User Controls, but I ended up just extending the URL rewriter, which was much easier than I expected.

My TemplateOverrideRewriteProvider allows you to do two things:

  1. Specify a custom ASPX file in a property called “CustomTemplate” (add it to your Page Types as a simple string property).  If this property exists and has a value, the URL rewriter will send the request there.
  2. Put an ASPX file in a specific directory, named for the page ID.  If this file exists (ex: “27.aspx”), requests for that page ID will use that ASPX file.  (Essentially the same as above, but you enable it by creating a file, rather then specifying the file name.)

The thing that made it easy is that we’re not hacking the whole URL rewriter.  In fact, we’re letting the core URL rewriter do its thing, we’re just tacking on some extra stuff at the end.  So, after we’ve called the base rewriting functionality, then we check to see if our page has a custom template specified, or if there’s an APSX page named for the ID.  If there is, we rewrite a little further to accommodate it.

The actual mechanics of it are really simple – the “ConvertToInternalInternal” (say that five times fast…) method takes an incoming UrlBuilder object.  This is where the request is currently heading.  You can do whatever you want with this, primarily by changing the Path property.  Change it to whatever you like, an IIS will send the request there instead.

This is in the works for us on a couple of projects.  We’re going to use it for situations where we need “application-ish” pages – like search results pages, for example – but we have a lot of logic and properties bound up in our standard Page Type, and we don’t want to recreate and manage a new Page Type just to enable a new template.  I’m thinking this will cut down our necessary Page Types on our average install by at least half.

To use it, compile it into your project, and register it in episerver.config.  You’ll need to add an element for it under “episerver/urlRewrite/providers” and specify it as the “defaultProvider.”  Check the comments in the code also, as there’s a little configuration – you need to specify the directory paths you want to use for your custom templates.

To represent the new concept behind this more appropriately, we’re dividing our template directories up like this:

/Templates/ByPageType
/Templates/ByPageId
/Templates/ByCustomMapping

The “ByPageType” directory is where the previously-standard templates went, to represent Page Types.  “ByPageId” contains templates named for page IDs, and “ByCustomMapping” contains templates that editors might enter in the “CustomTemplate” property.

 

TemplateOverrideRewriteProvider Download

Dec 08, 2010

Comments

Deane Barker
Deane Barker Dec 8, 2010 02:17 PM

Ugh. Sorry. Fixed.

Dec 9, 2010 08:58 AM

This is really cool!
Like you said for 95% the base functionality is enough, but for those 5% this is gold :)

Jarle Friestad
Jarle Friestad Dec 9, 2010 04:03 PM

Why not just use PageTypeBuilder? There you can share common properties through inheritance.


public class BaseEditorial : TypedPageData
{
[PageTypeProperty]
public virtual string MainBody { get; set; }

//and a bunch more
}

[PageType(Filename = "~/Templates/Pages/Article.aspx")]
public class Article : BaseEditorial
{
}

[PageType(Filename = "~/Templates/Pages/ArticleWithIngress.aspx")]
public class ArticleWithIngress : BaseEditorial
{

[PageTypeProperty]
public virtual string Ingress { get; set; }
}

Dec 10, 2010 11:16 PM

ConvertToInternalInternal” (say that five times fast…)

:)

tost
tost Dec 13, 2010 04:46 AM

This is pretty cool. You could use for handling mobile scenarios I guess. Could be quite useful. Good work!

Dec 16, 2010 08:23 AM

I was thinking. About investigating the same thing. Nice one!

Joshua Folkerts
Joshua Folkerts Dec 28, 2010 02:16 PM

After some testing, line 45 will need to be changed to this.
url.Path = pathToCustomTemplate;

Just a heads up.

anguyen@goldencomm.com
anguyen@goldencomm.com Nov 11, 2013 10:38 AM

Hi ,
I Could not able to download the TemplateOverrideRewriteProvider from above link. it is showing me 404 error. can you please provide a working link for it.

Alex Davies
Alex Davies Jan 10, 2014 01:16 AM

This sounds useful, can you please update the link, it is still giving 404s

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog