Understanding the Rendering Parameters

Being a Sitecore developer, you must have heard a term called “Rendering Parameters”. In today’s post we’ll together explore the Rendering Parameters with WWH (What, When & how) scenarios.

What is Rendering Parameter?

When to use Rendering Parameters?

How to use Rendering Parameters?

So I’ll keep this post as simple as possible and will explain you the concept of Rendering Parameters.

What is Rendering Parameter?

Rendering Parameter is a way to pass the additional item(s) to the rendering or component apart from rendering data source.

When to use Rendering Parameters?

Let’s assume, you are in a situation where content editors need to use some additional items on a component apart from its actual data source. For example – I am going to use Rendering parameter to pass an additional CSS class to the component, which will allow my content editors to choose the style of their component.

How to use Rendering Parameters?

Let’s jump to the exercise to understand the complete scenario and see how we can make use of Rendering Parameter.

This complete exercise is divided into 3 easy tasks. Which I’ll be explaining you one by one.

Task-1 Creating a Parameter Template

Create a new template and make sure this template inherits from a base template of standard Rendering Parameters – “/System/Layout/Rendering Parameters/Standard Rendering Parameters”

RP-1

And create the fields as per your requirement. In my scenario – I just need a single text field. See the below image.

RP-2

Task -2  Reference you parameter template

Reference you parameter template on your required rendering under the Editor Options section of your rendering, as shown below

RP-3

Once you reference your parameter template to your rendering, you can then check the same by going to the content item where you are using this rendering and go to the Presentation > Details > Final Layout and select your rendering and click edit – you should see your Rendering Parameter field there. See the image below

RP-4

You can notice here that your complete template field(s) and section are available here and Content Editors can make use of this to pass some additional data to the component or rendering.

Task -3 Read the parameter value in code

In my scenario, I am using MVC and used a controller rendering to make it work. So I’ll show the code to access the parameter value in MVC controller action.

 public ActionResult Index()
        {

            var item = Sitecore.Context.Item;
            var addtionalCssParameter = RenderingContext.Current.Rendering.Parameters["Background Class"];

            var model = new StandardPage
            {
                Title = item.Fields["Title"].Value,
                Description = item.Fields["Description"].Value,
                AdditionalCss = addtionalCssParameter
            };
            return View(model);
        }

Well, if you are using ASP.NET instead of MVC then I assume you must be using the Sublayout as renderings and that should not be a show stopper for you. You can still use the rendering parameters in the same way we use for MVC controller renderings. The Only task which is going to be a bit different is –  reading the parameter value in code.

So nothing to worry, here is the code to read the rendering parameter value from .ascx.cs file.

public string AdditionalCss
        {
            get
            {
                var sublayout = ((Sublayout) this.Parent);
                NameValueCollection renderingParameter = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);
                return renderingParameter["background Class"];
            }
        }

And that’s all about the Rendering Parameters.
I hope this post will help you in your further Sitecore journey. Happy Coding 🙂