How to Extend Templates in Sitecore CMS

February 26, 2015

Blog | Development | How to Extend Templates in Sitecore CMS
How to Extend Templates in Sitecore CMS

This is the 21st in a series of posts about learning how to use and develop for Sitecore CMS, and the follow-up post to How to Create Branch Templates in Sitecore CMS”.

In this post I will cover how to extend templates so that fields defined on a parent template can be reused on a child template. This is useful because it allows changes on one template to be automatically propagated to others. It will save developers time when making changes, and give content editors a more consistent editing experience.

Note:

This tutorial will expand on the templates that were created in How to Create Branch Templates in Sitecore CMS.

Example Setup

To properly demonstrate extending templates, you will need to add some fields to the templates we have. Add the following sections/fields to the “Page” template

  • Page Data
    • Title – single line text field
    • Content – rich text field
  • Metadata
    • Description – multi-line text field
    • Image – image field

The page template will be extended to create specialized templates.

Extending by Creating a New Template

The simplest and most common way of extending a template is done when creating a new template.

For this example, I’ll be creating an “Article” template that extends the “Page” template and adds a date field.

Select the /sitecore/Templates/User Defined folder, and create a new template in it:

Give the new template a name of “Article”.

Before clicking “Next” change the “Base template” field to point to the “Page” template:

Click “Next”, and leave the “User Defined” folder selected on the “Location” page. Click “Next” again and “Finish” on the last page of the create template wizard.

Note:

The new template will not contain any fields or sections. This is the expected behavior because it avoids needing to duplicate changes between templates.

Add some fields to the “Article” template.

  • Aritlce Data
    • Date – date field
    • Author – single line text field

Creating a new item from the “Article” template will show the fields from both the “Article” and “Page” templates.

Extending by Modifying an Existing Template

The other way to extend templates is to modify existing templates.

For this example, I’ll be modifying the “Home Page” template so that it extends the “Page” template.

Select the /sitecore/templates/User Defined/Home Page item and view the “Content” tab in the editor.

In the “__Base template” field, select /Templates/User Defined/Page and remove Standard template from the selection.

Save the changes. A warning will appear:

A confirmation dialog that says

Click “OK”.

Note:

In this case, no fields on the “Home Page” items are changing. Use good judgment when modifying templates. Never modify templates in a production environment as it may have serious consequences for the website.

Examining the “Home” item will show the fields from the “Page” template in addition to any that may be added to the “Home Page” template.


Check back in next week for the next tutorial. And, as always, if you have any questions or if there’s something you’d like to see covered, drop us a line.

Tim Leverett

Senior Developer
Tags
  • Sitecore

Recent Work

Check out what else we've been working on