How To Create Templates in Sitecore 7 and Use Them In Layouts?
Understanding Sitecore templates will probably be your first challenge. In every other CMS system, a template is something that is usually associated with an object that defines how something will look, normally a page that can be created within the editor.. however, in Sitecore, this isn't' the case. When I first started to work with Sitecore, I found it easier to refer to templates as data-templates instead. A Sitecore template has the same concept as a class in.NET. It's an object that you use to define something. Like C# classes, templates are made up of fields. These fields can be text, numbers, dates, rich-text etc... 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 TemplateTo find a list of the templates in your project, open up your Sitecore content editor and navigate to the templates section '/sitecore/templates' : 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/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/CommonTo create a new template on your new 'My Website' folder, right click and select 'New Template' 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 templateAfter 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. 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.