Templates

The Basics

Templates are the foundation for all content in Sitecore. While formally called "data templates," most developers simply refer to "templates" in the lexicon. Templates are so critical to Sitecore that they get a special node in the Content Tree:

The Template node of the Sitecore content tree.

The Template node in the Sitecore Content tree.

Quite simply, templates provide schema for content. Schema gives structure and definition to content.

A template is composed of fields, and each field has a data type. Sitecore provides dozens of basic types (Single-Line Text, DateTime, Image, etc.), several complex types (Multilist, General Link, Droplink, etc.), and allows for custom types that are implemented by developers. Templates can be simple (with only one field) or complex (with dozens of fields). When designing a template, it is important to strive for simplicity - fewer fields are better than many fields.

The Template Builder (Sections & Fields)

The Template Builder is a special interface in Sitecore for creating templates. Template sections and fields are created and edited in rows. Each row is made up of the field name, data type, and source:

The input fields on a single template field.

One row of the template builder. Each row represents a template field.

The builder columns from left to right are:

  • Name: The name of the field. This will be displayed by default to Content Authors. When referencing a template field in code, this is the name
  • Type: Data type of this field.
  • Source: A location in the content tree to use as the source of data (read: dropdowns, lists, etc.).
  • Unversioned: Determines if this field's data will be shared across all numbered versions.
  • Shared: Fields marked as 'Shared' will update across all language versions of the Item.

Templates also have the concept of sections. Sections group fields together - nothing more, nothing less. These sections are reflected in the Content Editor:

The Template Builder showing section being added.

Sections being defined on a template.

An item with template sections collapsed and expanded.

Expandable/collapsable template sections in the Content Editor.

Use sections to provide organization and semantic meaning to related fields. It's a small usability win for Content Authors.

Inheritence

Templates in Sitecore can inherit from one another, which introduces a powerful concept to Sitecore information architecture.

In the following example, "Person" and "Employee" are inherited into a "Team Member" template:

The Template Builder showing fields from the Person template.

The Person template.

The Template Builder showing fields from the Employee template.

The Employee template.

Template composed of both Person and Employee templates.

Team Member template. Notice that this template consists of fields from both the Person and Employee templates.

Templates play an important role in the encapsulation of content into reusable components. By spreading content across one or more templates, natural boundries are defined that direct the overall implementation of components. For example, a "Header" template might have two fields ("Title" and "Subtitle"), but a "Banner" template might have these and more ("Title," "Subtitle," "Background Image," "Display Size", etc.). Should the Banner template be broken down into two separate templates? Just like the Team Member example above, it could be. In the end, it's a fundamental architecture question that each project will need to answer, but it is possible to solve this challenge through inheritence.

Because C# does not feature multiple inheritence, it is important to consider implementation strategies that account for Sitecore templates that do have multiple inheritence. Third-party ORM Glass.Mapper has awesome support for using C# interfaces (which do support multiple inheritence) and proxy classes to support Sitecore's multiple inheritence concepts.