Create Rendering Parameters in Sitecore MVC
Posted 09/19/2016 by Yogeshwer Sharma
While working on pagination control I got a requirement to create rendering parameters on rendering so that we can easily handle the values like enable pagination, ResultOnPage and others from rendering itself and from page editor.
Let me start with what are Rendering Parameters in Sitecore:
Rendering parameters can be used to pass parameters to sitecore presentation components. They are used to control the presentation of a dynamic component from page editor.
In the below example I’ll demonstrate to how to create rendering parameters and how we can read those parameters in code.
Create Rendering Parameters in Sitecore -
- Create a data template under /sitecore/templates/ or any location where you want to have that template. I have created a folder template with name Rendering Parameter and under that template created new template with name _paging.
Create fields on that template which you want, in my solution I have created two fields - You can create any fields with any name which you want to use as rendering parameters.
- Enable - Checkbox type
- ResultsOnPage – Single-Line-Text
- Inherit the Standard Rendering Parameters template under /sitecore/templates/System/Layout/Rendering Parameters/Standard Rendering Parameters location.
- Select the rendering Item on which you want to use these custom parameters and navigate to Parameters Template field which is in Editor Options section.
- Now apply this rendering to the content item and open rendering properties you can see fields on rendering which you created in data template.
- You can provide values in these fields.
How to read the Rendering Parameter value in code –
My solution was in MVC so I got these values in controller with the help of below code –
var enable = RenderingContext.Current.Rendering.Parameters["Enable"];
var ResultOnPage = RenderingContext.Current.Rendering.Parameters["ResultOnPage"];
The main advantages of rendering parameters are we can easily manage/handle dynamic values from page editor.