This is the fifth post as part of Sitecore Template series.
In last three posts, we discussed about field types, now let’s explore what is template Inheritance, and the benefits it provide.
Template is a collection of fields, where fields can be defined on a template, or can be inherited from other templates. Similar to Object Oriented Programming, where a class can be inherited from another class, in Sitecore, a template can be inherited from another template.
Each template can inherit fields, field sections, standard values, and potentially other properties from any number of other templates.
Sitecore uses lot of template inheritance, for system templates. There is a Standard Template, which defines field sections, and fields common to all items.
Whenever we create any new template, it will always inherit from the Standard Template, and all the System Fields comes with it.
Why do we use Inheritance?
In general, we use Inheritance, for reusability. Where we can define the properties, & methods in one common place, and access it from all other places. for Maintainability, where it is easy to make modification at single location.
Sitecore Template Inheritance Benefits
- Reusability: We define common fields in one template, and inherit in all other templates. This avoids duplication.
- Maintainability: Modify the fields in the main template, all the templates automatically gets the modifications.
Let’s look into a Scenario, where we have Article template, and Podcast template, both are having certain fields common, like Title, Date, and Description etc. What if we create a new base template, with all these common fields, and inherit it, in both Article and Podcast template. In this way we can remove the duplication, and increase the reusability.
- If you directly or indirectly inherit from the same template more than once, Sitecore behaves as if you had inherited that template only once.
For example, Template B inherits from Template A, Template C inherits from both Template A & B, here indirectly we are inheriting Template A (once directly, and another indirectly from B), multiple times. But Sitecore inherits Template A only once.
- One template, can only inherit one set of presentation details. If a template inherits from two templates, and both have presentation details, only the first template’s presentation details in the inheritance list will be used. (In the coming post, we will explore more about presentation details)
- If two templates are having same field section name, then when a template inherits from these two templates, similar field sections are merged, and all the fields under them are consolidated.
- We should avoid Circular templates. for example, Template A can inherit from Templates B and C. Template C can inherit from Template A, too.
- Professional Sitecore Development
- Professional Sitecore 8 Development: A Complete Guide to Solutions and Best Practices
Happy Learning :)