How To Create Templates in Sitecore and Use Them In Layouts?

Templates are the building blocks of every Sitecore project. Unlike the normal CMS terminology, a Sitecore template represents the data items that define the properties of our pages and renderings. In today’s tutorial, I’m going to cover the how to create a template in Sitecore and use it on your website.

Creating a Sitecore Template

To find a list of the templates in your project, open up your Sitecore content editor and navigate to the templates section ‘/sitecore/templates’ :

Sitecore_creating_a_templates_1

In here you will find a list of default Sitecore system templates. On a usual project, it is usually good practice to create a new folder called ‘Sites’ or ‘User Defined’.

/sitecore/templates/Sites/[Website Name]

or

/sitecore/templates/User Defined/[Website Name]

In your ‘Sites folder, you then create a new folder called your website’s name. Splitting up your templates is a good idea in case you use things like object mappers and editor permissions. The more you split your controller up, the greater control you have on your Sitecore instance.

If you run two websites in your Sitecore instance and want to share pages, or blocks between the two, then most people define these templates in:

/sitecore/templates/Common

To create a new template on your new ‘My Website’ folder, right click and select ‘New Template’

Sitecore_creating_a_templates_2

From here you will be presented with the ‘New Template’ screen. The useful thing about this screen is the ability to base your template off of another template. This can be really useful if you want to add shared data between your pages. For example (this isn’t the way I’d structure a project) you could create a base page that contains everything you need for your header and footer, for each page template you define inherit from the base page to import the header and footer settings. This technique can save a lot of duplication when you start building your website in anger!

By default, the standard base Sitecore template is the ‘standard template’ and any new template you create will default to:

/sitecore/Templates/System/Templates/Standard template

After you decide the name and the base template, you need to decide where in the content tree to save your template. As mentioned above I always prefer to organise my controls in a website folder with-in individual folders.

Sitecore_creating_a_templates_4

After you create your template, you can start doing useful things with it. You can now add sections and fields using the ‘Builder’ tab. As the names suggest these will be the items that will define what properties a content editor can add about a page. Template sections are used to group similar fields together, fields are the properties to define data with. After you save the template, it can then be used to create content items in Sitecore.

Creating an Item In Sitecore using a Template

Items created from a template will have the template sections and fields defined in the template. To create an item based on a template, in the Content tree, if you right click on a folder and select ‘Create From Template’

Sitecore_creating_a_templates_5

You should see be able to add content to your new template. As you can see from the screenshot, the properties defined in the template are now available for a content editor to add data to.

Sitecore_creating_a_templates_6

How To Change The Template Of An Existing Sitecore Item

There are times when you may want to change the template of an existing site core item. Doing this is simple.

Sitecore_creating_a_templates_7

Click on the item you want to change, and in the top ribbon, select the ‘Configure’ tab. In the ‘Template’ section, click ‘Change’. From here you can then select a new template to use.

Conclusion

In today’s guide, we’ve covered the basics of what a template is and how you can create one in Sitecore. When dealing with items in the content tree, it is a good idea to spend some time and consider how you want to structure your project. The better you structure your projects, the better your experience with Sitecore will become.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *