Rendering Parameters and Rendering Parameter Templates in Sitecore

Heythere! Welcome to "Sitecore Series"

Post is all about sharing the details about rendering parameters and accessing rendering parameters in code.


What are rendering parameters?

Rendering parameters are used to pass some extra data to a rendering and to give some control to content author on rendering display.

Scenarios:

1) To handle a requirement , where the rendering to be displayed with different background colors.

2) To display a component in different ways based on the value selection.

3) Applying different css classes to the rendering.


1) How to Create Rendering Parameters:

Let's take a look at below steps to understand the rendering parameters creation.

As we all know the data templates are created from "standard template" but for rendering parameters the template to be created from  "Standard Rendering Parameters Template"


Here is the path for the Standard Rendering Parameters Template:

Templates/System/Layout/Rendering Parameters/Standard Rendering Parameter Template


Create a new rendering parameter template from the "Standard Rendering Parameter Template"

a) Right click on template folder - > click on "insert from template"

b) Specify the template name and in base template, choose the standard rendering parameter template specified in above path.



c) Click on next -> choose the folder to create the template -> click on ok

d) Access the template -> navigate to builder section

e) Specify the template section

f) Specify the required fields. (For ex: To update the color of rendering background, use a drop down field and specify the data source path where it contains all the required colors.)

g) Save the template.


2) Refer Rendering Parameter Template in rendering:

Once the template is created, next step is to link the rendering parameter template to rendering. Follow below steps to refer the rendering parameter template in the rendering.

a) Navigate to rendering under layouts - > renderings.

b) Open the rendering and access "Parameters Template" field

c) In the "Parameters Template" field, choose the template created in previous step (step 1).

d) Save the changes


3) Update rendering parameter values in rendering presentation:

a) Navigate to the page, where the rendering to be added.

b) Add the rendering in the presentation 

c) An extra section will display contains the field section and fields created in previous step (step2)

d) Provide the data in rendering parameter fields.

e) Save the changes


4) Accessing rendering parameter values in code:

Now we need to handle the code, based on the values added in step 3.

Refer below piece of code to access rendering parameters and it's values in code.


var renderingparams = Sitecore.MVC.Presentation.RenderingContext.CurrentOrNull;
if(renderingparams != null)
{
	var rp = renderingparams.Rendering.Parameters;
    string color = rp["color"];
}
// now the color contains the value provided in rendering parameter added in rendering presentation.
// do the changes as required


Hope this post helps you. Happy Sitecoring... 😊.

Comments

Popular posts from this blog

Overwriting sxa content field in solr document

Displaying Custom 404 error page for urls with unallowed file extension in sitecore

How to specify custom richtext menu as default richtext field setting when no source is specified for richtext field in sitecore

How to rebuild custom solr index programmatically in Sitecore

Handling URL requests with different file extensions in Sitecore

Accessing Items from Sitecore Database in different scenarios