I found out that many Sitecore Developers never used Sublayout Parameter Templates, so I decided to make a post about them.
Imagine that you have a situation in which you need to pass items from different datasources to a single stand alone sublayout. You obviously cannot use the Sublayout Datasource parameter and the content editors (especially new ones) might find the whole concept of adding Item IDs to the additional parameters a bit confusing. In such situations the Sublayout Parameter Templates come in handy.
Here are the simple steps that you need to execute in order to leverage the Sublayout Parameter Templates.
Step 1 – Creating a new template
Create a new template that uses “/System/Layout/Rendering Parameters/Standard Rendering Parameters” as a base template.
Step 2 – Creating the template fields
Create the template fields the same way you will do with any normal template. By using Standard Rendering Parameters as a base you won`t lose any of the Standard Template behavior your are used to.
Step 3 – Attaching the new template to the sublayout
Attach your new template by using the “Parameters Template” field on your Sublayout.
Step 4 – Try if it works and add some data!
To add some data you just need to add the Sublayout to the presentation details and edit its settings.
Step 5 – Handle it in code!
Sublayout sublayout = Parent as Sublayout; NameValueCollection sublayoutParameters = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters); string title = sublayoutParameters["My Component Title"]; Item firstDatasource = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(sublayoutParameters["My First Datasource"]));
And you are all set !
When dealing with Sublayout Parameter Templates (or Sitecore in General) – please consider using ORMs like Glass Mapper to ease your work.