Sitecore Rendering Parameters Best Practices – Part Two
In the Sitecore Rendering Parameters Best Practices – Part One we discussed how to properly use rendering parameters and looked at some examples of issues one would run into using the free-entry name-value parameter fields in the Parameters section of rendering properties. In this post, we will look at how we can address these issues using Parameter Templates.
It is indeed interesting that this feature has been in Sitecore for some time, however, not many developers know about it. Have you ever wondered through all the fields on a rendering definition item and asked yourself – “what are these settings used for?” Although, many are very useful and I personally encourage every Sitecore developer to get to know all of them, in this post we will focus on the Parameter Template field.
Custom Parameter Templates inherit from the /sitecore/templates/System/Layout/Rendering Parameters/Standard Rendering Parameters. These templates allow us to create definitions for parameters in the same way we use item data definition templates for items. In Parameter Templates we can define sections and create type-specific fields, with validation, help messages and other good things we are used to in Sitecore data templates. Once the template is created, it can be referenced from the Parameters Template field of any rendering.
After assigning a Parameter Template we can navigate to the item Presentation Layer Details view and change these settings from the rendering properties window. Notice that although we have added a custom parameter section, the original Parameters section hosting free-text entry name-value fields is still present. Why? Well, while I would discourage using it for changing rendering settings, it gives more flexibility to developers, and I personally use it for development purposes. Since free-text variables can be easily edited, added, and removed, I prefer using them for development purposes, for instance, to enable tracing for a component, enable logging, or switch databases, well you get the point.
The parameters set using Parameter Templates are passed in the same way as the ones from the Parameters section – a collection of name-value pairs, thus, can accessed in exactly the same way through code (Notice: that the values carry raw values of the fields).
public static bool IsTabRandomizationEnabled { get { var params = SitecoreHelper.CurrentRendering.Parameters; return params.Contains(Constants.Params.EnableTabRandomization) && params[Constants.Params.EnableTabRandomization] == "1"; } }
Parameter Templates extend the great flexibility of Sitecore renderings even further by allowing Sitecore control use, validation, help messages, and security making Content Editors experience much more cohesive.