Friday, February 26, 2016

Rendering Parameters

Hi all,
Today we discuss about Renderings parameter in sitecore.
So first question come in mind what is rendering parameters & what’s the use of that.
Rendering parameters:
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.
Sitecore developers can easily extend the properties on a presentation component. This is done using a special Data Template that defines the structure of custom properties and
inherits from the Standard Rendering Parameters template. The new special parameters template is then assigned to the definition item of the component.

I cover these topic in this blog:
  • Create a Parameters Template
  • Assign a Parameters Template to a component
  • Use the Content Editor to set parameters on a component
  • Output the contents of the parameter field

Create parameter template:
  1. Navigate to template section where you want to add parameter template.
  2. Create a new template & set base template to Standard Rendering Parameters.

     
  3. Name the field section ex: Styles and create a field named Class of the type Droplink and set the source to /sitecore/content/settings/styles/widgets.



  4. widgets child item is made by a template that holds a CSS Class field like shown below.

  5. That CSS Classes field hold the css class name.
  6. Now we need to navigate to the sublayout on which we need to define rendering parameters.
  7. Find the Parameters Template   field in Editor option and click browse.

  8. Locate & select the Rendering parameter template which we create earlier.
  9. Save
  10. When we edit the presentation detail , you can see the rendering parameter value on specific sublayout on which we set Parameters Template field.

  11. Now you are able to pass rendering parameter, now time to retrieve using code.
  12. First using visual studio navigate to the sublayout for which you setting the rendering parameter.
  13. Retrieve the sublayout’s parameter.
  14. Extract class parameter from parameter collection.
  15. The Parameters Template you created earlier defined the Class parameter as a Droplink. The value of the Class parameter is going to be a string containing a Sitecore ID (the raw value for droplinks).
  16. Use this ID to get the corresponding item from the context database. Remember the GetItem method accepts IDs as strings; however, it will throw an error if the string is either empty or null.
  17. The referenced item has a field named CSS Classes. Retrieve the raw value of this field as a string and make it available as a public property.
  18. Sample code are shown below.

#RelatedArticles.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RelatedArticles.ascx.cs" Inherits="Training.layouts.BaseCore.content.RelatedArticles" %>
<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>
<div class="four columns">
    <div class="<%=CSSClass %>">
        <h3>
        Related articles
        </h3>
               <asp:Repeater ID="rpArticles" ItemType="Sitecore.Data.Items.Item" runat="server">
                    <HeaderTemplate>
                             <ul class="relatedArticles">
                     </HeaderTemplate>
                                    <ItemTemplate>
                                                <li>
                                                    <a href="<%#:Sitecore.Links.LinkManager.GetItemUrl(Item) %>">
                                                           <sc:Text runat="server" Field="Page Heading" DataSource="<%#: Item.ID %>"/>
                                                    </a>
                                                </li>
                                    </ItemTemplate>
                    <FooterTemplate>
                             </ul>
                     </FooterTemplate>
                        </asp:Repeater>
            </div>
</div>

#RelatedArticles.ascx.cs
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Web.UI.WebControls;
using System.Collections.Specialized;

namespace Training.layouts.BaseCore.content
{
    using System;

    public partial class RelatedArticles : System.Web.UI.UserControl
    {
        private void Page_Load(object sender, EventArgs e)
        {
            MultilistField objmuMultilistField = Sitecore.Context.Item.Fields["Related Articles"];
            rpArticles.DataSource = objmuMultilistField.GetItems();
            rpArticles.DataBind();
            var sublayout = ((Sublayout)this.Parent);
            NameValueCollection parameters = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);
            string id = parameters["class"];
            if (!String.IsNullOrWhiteSpace(id))
            {
                Item itm = Sitecore.Context.Database.GetItem(id);
                CSSClass = itm.Fields["CSS Classes"];
            }

        }

        public Field CSSClass { get; set; }
    }
}
After that just Deploy the visual studio & see the use of rendering parameters by passing the css class value.

Ex:1.

Its show something like that:


Ex:2.

Its show something like that:

Conclusion: So by this blog we can understand that, we can pass value at run time by using rendering parameters for dynamic look of our website,
here we also can pass more parameter according to need and also capable for access them via code.




I'll try my best & hope you will enjoy that blog, for any concern & query 
please let me know

Thanks
Arun Sharma

No comments:

Post a Comment