Versions used: Sitecore 8.1 rev. 160519 (Update-3). Synthesis 8.2.0
One of the features provided by Sitecore to enrich content entry and separate concerns are Rendering Parameters. Rendering parameters should be used to empower editors to customise a component’s behaviour, look-and-feel, or even functionality without changing its data source.
If you are not familiar with or not using rendering parameters in your solution, I’d suggest you read about it and start using them immediately. This post is about a Synthesis helper that fetches and casts rendering parameters (when using Parameters Template field); NOT on how to use or implement rendering parameters.
In one of my previous projects, we were using rendering parameters heavily and decided to create a little helper that would return a specific component’s rendering parameter. As I had used glass before, and knew about GetRenderingParameters, I decided to check the source code to see how I could recreate something similar to use with synthesis. It turned out I was able to port the code without needing to make many changes so shout-out to the Glass team.
Here’s the code implementantion with comments.
The helper can be used like this:
var renderingParam = GetRenderingParameters<IRenderingParamItem>(RenderingParam.ItemTemplateId);
IRenderingParamItem is the interface type of your rendering parameters template and
RenderingParam.ItemTemplateId is the ID of said template.
Note: This helper is not checking for null, you should check for null doing something like:
if (RenderingContext.Current.Rendering != null && RenderingContext.Current.Rendering.Parameters != null && RenderingContext.Current.Rendering.Parameters.Any())
Please let me know what you think and/or if you can spot any errors.