Sitecore
Templates
A Sitecore template is an item which refers to a structure of
other items. A template may also define the sections and fields into which it
breaks down into and fulfils the content. Each section represents a sub-item
under the template and every field is another sub-item under the item of the
related section. Each item in Sitecore is created from some template.
Types of Templates
Data Templates
Data templates are the schema for
Sitecore content item. It defines the data and all the information that you
want to display on a page. Each content item in Sitecore is an instance a data
template. Data templates can inherit from other data templates and can be
assigned Presentation Details and insert options.
Branch templates
A branch template allows you to
define the content structure which can contain a single item, a hierarchy of
items, or multiple hierarchies of items. So by creating an instance of a branch
template the whole item structure appears without the user having to create
them one by one.
Command templates
Command templates allows the automatic creation of items according to
the business logic rather than the predefined structures like data templates.
It defines a class and method which is to be called during and insert operation,
so that means when we try to create an item using that template the associated
class method would get executed.
Command template can be created, using
/System/Branches/Command Template.
Command template requires the custom code which handles the functionality
you want to execute with the command template and some configuration into
Command.config file
Rendering Parameter template
Rendering parameters are used to pass
parameters to presentation components to decide how to display or present data
on a page. They are used to control the presentation of a dynamic component
from page editor.
Rendering Parameter Templates, which
allow the template author to specify named parameters which will appear in the
dialogue used to configure presentation component properties and the data type
of these parameters.
For example – Suppose you want to
give an option to the content editors to choose the style of their component,
in that case you can use Rendering parameter to pass an additional CSS class to
the component.
/sitecore/templates/System/Layout/Rendering
Parameters/Standard Rendering Parameters
Inheritance
Inheritance is one of the best
feature used in sitecore. As you know templates can be inherited from one or
more other templates. When we create any new template it is always inherited
from sitecore standard template. We can define abstract templates commonly to
more data templates and so the content items can contain a number of fields
without always having to re-define them separately for every template.
There are some best practices which should keep in mind while template
inheritance
1)
You should always avoid template inheritance more
than two levels.
2)
Split templates into different folders
Standard template
When a new template is created in
sitecore it is always inherited from a template named Standard template. This
template does not have any section or fields itself but it inherits from other templates,
which helps us to define the display name, insert options, Item Buckets,
Security, layout details, publishing restrictions and workflow, etc. on the
items. All of the fields inherited into standard template begin with__.
The standard template is found in
/sitecore/templates/System/Templates/Standard template
__Standard
Values
Standard values provide initial
values or default values for all the items based on a given data template.
Every data template can have their standard value item where the item can have
the default values for every fields in that data template or its base template.
You can also set Layout details, Insert options, initial workflows etc on the
standard value item. Each data template can have only one standard value item.
You can create __Standard Values using the Standard values button
in the Ribbon on the Builder Options tab. In order for the Builder Options tab
to appear, you have to choose a template from the branch in the left part of
the window and switch over to the Builder tab in the content part of the
window.
Versioned,
Unversioned and Shared
Versioning of an item is controlled in three ways:
Versioned fields
The versioned field value is always different across different numbered
version and different language as well. By default, all the fields are versioned.
To make a field versioned do not tick shared and unversioned checkbox while
creating the template.
Unversioned fields
The unversioned fields share same data between all the numbered version
but have different data for each language version. If we want to have such kind of data, we should mark a field unversioned
in template definition.
Shared fields
The shared field value shares same data among all the numbered version
and all languages. So if you make a change in one language version of an item
it would reflect in other languages and their numbered versions. You just need to mark the field shared in template definition
Fallback
Fallback is a mechanism which ensure
the inserting of a field value from another language or from another version,
if the field is empty. The fallback hierarchy of individual languages is
defined in /sitecore/system/Languages.
It is necessary to define this detailed setting of fields on the
individual item fields. Only the basic settings are available on the Builder
tab on the template.
Recommendations
when Creating Templates
Follow convention. Sitecore has prepared a document of recommended
principles when designing and implementing Sitecore solutions, called Sitecore Helix. I recommend you read it. It
will probably help you design all fields that you encounter in Sitecore. The
section related to templates uses these classifications:
·
Interface template – contains fields,
the convention is such that it begins with _, in C# it is an equivalent to
interfaces.
·
Page type template – can have a
layout, does not contain fields, inherits from one or more interface templates,
instances are created from it, in C# it is equivalent to a class that
implements one or more interfaces.
·
Datasource template – do not have
layouts even though they are also inherited from the interface template, they
are only used for referenced items as datasource for renderings.
·
Settings template – templates define
the settings for the implementation of business logic, do not inherit from the
interface template and are directly instanced.
·
Folder template – Do not use
Common/Folder! Have your own folder template for every module. Thanks to this,
you will be able to modularize the solution and use various insert options,
authorizations, etc. on the file.
Do not use the Shared or Unversioned field unless it is absolutely
necessary. It is always necessary to consider the language and cultural context
carefully.