Posted in: Comments

Sometimes you want a small variation of a block. E.g. in one content area you want the teaser blocks to have headings and in another area no headings. You basically have two options to achieve this; use tags to select another renderer or the new layout feature in EPiServer 7.5 (which basically works as tags). Both options involves using a new renderer and you have to repeat most of the functionality in the alternative renderer. This is often unnecessary and doesn’t comply with the DRY principle.

Instead we could just pass a custom rendering setting to the rendering control:

<EPiServer:Property PropertyName="SidebarTeasers" runat="server">
	<RenderSettings ShowHeadings="true" />
</EPiServer:Property>

Unfortunately there is no way to retrieve these settings Out-of-the-Box in EPiServer, at least not that I know of. But this is quite easy to solve by just traversing the control tree and get the Property Data Control, at least in webforms.

First we need the Property Data Control:

private PropertyDataControl propertyDataControl;

protected PropertyDataControl PropertyDataControl
{
    get
    {
        if (this.propertyDataControl == null)
        {
            Control parent = this.Parent;

            while (this.propertyDataControl == null && parent != null)
            {
                this.propertyDataControl = parent as PropertyDataControl;

                parent = parent.Parent;
            }
        }

        return this.propertyDataControl;
    }
}

And from the Property Data Control we can get the rendering settings:

protected IDictionary<string, object> RenderSettings
{
    get
    {
        if (this.PropertyDataControl != null)
        {
            return this.PropertyDataControl.RenderSettings;
        }

        return new Dictionary<string, object>();
    }
}

The complete code-behind for the block control with the ShowHeadings property:

namespace DV.Views.Blocks
{
    using EPiServer.Core;
    using EPiServer.Web;
    using EPiServer.Web.PropertyControls;
    using System.Collections.Generic;
    using System.Web.UI;

    public partial class TeaserControl : BlockControlBase<TeaserBlock>
    {
        #region Fields

        private PropertyDataControl propertyDataControl;

        #endregion

        protected IDictionary<string, object> RenderSettings
        {
            get
            {
                if (this.PropertyDataControl != null)
                {
                    return this.PropertyDataControl.RenderSettings;
                }

                return new Dictionary<string, object>();
            }
        }

        protected PropertyDataControl PropertyDataControl
        {
            get
            {
                if (this.propertyDataControl == null)
                {
                    Control parent = this.Parent;

                    while (this.propertyDataControl == null && parent != null)
                    {
                        this.propertyDataControl = parent as PropertyDataControl;

                        parent = parent.Parent;
                    }
                }

                return this.propertyDataControl;
            }
        }

        protected bool ShowHeadings
        {
            get
            {
                bool showHeadings = true;

                if (this.RenderSettings.ContainsKey("ShowHeadings"))
                {
                    bool.TryParse(this.RenderSettings.ItemAs<string>("ShowHeadings"), out showHeadings);
                }

                return showHeadings;
            }
        }
    }
}

And the front-end:

<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="TeaserControl.ascx.cs" Inherits="DV.Views.Blocks.TeaserControl" %>

<EPiServer:Property PropertyName="Heading" CustomTagName="h2" Visible="<%# this.ShowHeadings %>" runat="server" />

<EPiServer:Property PropertyName="Content" runat="server" />