Dont Fight The Framework – Please don’t base your Sitecore code on exact templates

Ok, so this will probably seem like a rant at first, and to some it will be preaching to the converted, but I cannot count the number of times that I have seen this approach in code.

if(item.TemplateName == "my template")


if(item.TemplateID == new ID(""))

In Sitecore, we have the great strength and flexibility that is template inheritance, and multiple inheritance at that. Most of the components we develop often contain logic that checks for the availability of field values and the like, and lots of code I have seen created by developers will have direct references to a particular template – please don’t :D.

In most cases, to be honest, in our code, we will utilise base classes to provide functionality for our application, we do in most cases to allow a Open Closed Principle in our programming. The same should be the case in our approach to Sitecore Templates, yes – right now – you need to know if the context item / data source is in fact a news item, advance that 6 months into the future when the user has now asked you to have video available to some news pages, and the pre-populated with some presentation in others. Then take this a step further, your code to find ‘all news articles’. All of this is so much easier when your code relies on the item having a base template, not relying on an exact one.

Here is a simple method that allows you to see when an item has a base template

        public bool HasBaseTemplate(Item item, ID templateId)
            if (item == null || templateId == null as ID)
                return false;

            // The template for the derivation test. What we want to derive 'from'
            Template basetemplate = TemplateManager.GetTemplate(templateId, item.Database);
            Template itemtemplate = TemplateManager.GetTemplate(item);

            return basetemplate != null && itemtemplate != null
                   && (itemtemplate.ID == basetemplate.ID || itemtemplate.DescendsFrom(basetemplate));

You could easily wrap this with an extension method or the like to make it sweeter still in general use, but the principle still holds true. By relying on a base template for the most part, when you decide to implement a super item later down the line, you won’t have to go finding and adding code that states ‘if this is news item or super news item or news video item’. Of course there are SOME occasions that require you to check for an EXPLICIT template, but I find this to be the exception rather than the rule.

So once I again I ask – Please don’t base your Sitecore code on exact templates (unless you really have to).

Related Articles

This article is part of the Don’t Fight The Framework Series

For more on template inheritance, please read Simon’s post on Sitecore Template Inheritance

2 thoughts on “Dont Fight The Framework – Please don’t base your Sitecore code on exact templates

  1. Pingback: Sitecore template inheritance | cardinalcore

Leave a Reply to Thomas Eldblom Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s