How I structure Sitecore Templates

Templates are the building blocks for a Sitecore installation, they’re amazingly flexible and with capabilities such as inheritance you can produce an elegant architecture, or alternatively a complete mess. Here’s how I like to structure things:

Base Templates

BaseTemplatesFirst off I have a folder of base templates. These are the building blocks for all the fields that will end up in a component data source or an actual page via inheritance. By setting them up as a base template we can re-use the same field definition for things like headings, but more importantly it keeps them focused on a specific purpose rather including details of the entire eventual item.

One additional rule I have for base templates is that they should only contain one data section within them. This in turn helps keep them focused on a specific purpose.

Page Templates

PageTemplatesNext we have pages. You guessed it, a page template is what a content editor will create an instance of when they make a page. It’s responsibility is to bring together default presentation details, the set of fields on the page and insert options for the pages beneath it.

All fields are inherited from base templates and standard values are used to define the defaults for the page.

Additionally to make the CMS experience as easy as possible for the content editors an icon should be set so that a page type can be visually identifiable.

Component Templates

ComponentTemplatesA component template is the equivalent of a page template but for data sources.

Like page templates all the fields are inherited from base templates and standard values are used to define the defaults for the page. An icon should also be set to make content types easily identifiable by content editors.

Folder Templates

FolderTemplatesFolder templates are often overlooked but they are an essential part of creating a decent user experience.

Folder templates are created to define the insert options for components and site settings rather than having them set on the site content tree.

Where relevant a folder template should also include itself as one of the insert options so that content editors can organise their content into sub-folders.

Parameter Templates

ParameterTemplatesWhen a component needs some config to customise its look and feel that is not content, it can be better to use Rendering Parameters rather than a data source.

Site Setting Templates

SettingsTemplateA Site configuration template is useful to contain various global settings for a site. This could include things such as the Site Logo, Google Analytics account details etc. Settings should be segmented into logical sections that have been defined in Base Templates and then inherited.

It can also be useful to separate some settings into their own template item depending on the scenario of what the setting is for.

Group Into Folders

With each these template types created, you’ll end up with a tree structure that looks something like this.

AllTemplates

Advertisements

Pipelines – remember the big picture

Sitecore pipelines are great. With them you can relatively easily add and remove functionality as you wish. Pipelines like httpRequestBegin, httpRequestProcessed and mvc.beginRequest are also really useful if you need some logic to run on a page load that shouldn’t really be part of a rendering. This could be anything from login checks to updating the way 404 pages are returned. However you do need to remember the big picture of what you are changing.

Pipelines don’t just effect the processes of the website your building, Sitecore uses them too. That means when you add a new processor to the httpRequestBegin pipeline, that’s going to effect every request in the admin cms too. Just checking the context item also isn’t enough as some things. e.g. opening a node in a treeview, will have the context of the node you clicked on!

Adding this snippet of code to the begining of your process should keep you safe though.

using Sitecore.Diagnostics;
using Sitecore.Pipelines.HttpRequest;
using Sitecore;
using Sitecore.SecurityModel;

namespace CustomPiplelineNamespace
{
    public class CustomPipeline : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            //Check args isn't null
            Assert.ArgumentNotNull(args, "args");
            //Check we have a site
            if (Context.Site == null)
                return;
            //Check the sites domain isn't one for sitecore
            if (Context.Site.Domain != DomainManager.GetDomain("sitecore"))
                return;
            //Check that we're not in a redirect
            if (args.Url.FilePathWithQueryString.ToUpperInvariant().Contains("redirected=true".ToUpperInvariant()))
                return;
            //Check that we're not in the page editor
            if (Context.PageMode.IsPageEditor)
                return;

            // DO CODE
        }
    }
}