Multi Site
Sitecore Dynamic Placeholders: Wrong placeholder settings or placeholders not showing
January 2, 2016
0

We regularly use dynamic placeholder within our projects to support a dynamic layout without running into the limitations of placeholders and the requirement that the name(more specifically the full path) of a placeholder must be unique on a page. In most cases the common implementations which can be found on the web work perfectly but in some cases you run into issues like a placeholder is missing on a page whilst the placeholder settings are available, or you get to see the wrong placeholder settings when adding a rendering to a page. In this blog post i will address how this issue can be resolved when implementing one of the more common dynamic placeholders implementation.

 Common dynamic placeholder implementations

Like indicated in the introduction of this blog post there are a couple of implementations for dynamic placeholders which can be found. The implementation on which this blog post is based consists of the following implementation:

 The issue with this implementation

The issue with this issue occurs when working with a placeholder key that is a path. Per example you have a rendering with placeholder called “column1” which is placed inside a placeholder called “body” your full placeholder key is : “/body/column1”.  This allows you to set placeholder settings for a specific placeholder path or for only the placeholder “column1”.

When you use this path as a key the current implementation would make the dynamic placeholder key “/body_00000000-0000-0000-0000-000000000000/column1_e33898de-6302-4756-8f0c-5f6c5218e02e” and the final key which will be used in finding the settings would be “/body_00000000-0000-0000-0000-000000000000/column1” instead of “/body/column1” which disables us from using the path as a placeholder settings key.

This happens because of the following piece of code:

 
            string placeholderKey = args.PlaceholderKey;
            Regex regex = new Regex(DYNAMIC_KEY_REGEX);
            Match match = regex.Match(placeholderKey);
            if (match.Success && match.Groups.Count > 0)
            {
                placeholderKey = match.Groups[1].Value;
            }
            else
            {
                return;
            }

In the highlighted part of the code it first matches for a string ending in a guid and after that it takes the second group in the match which is the full string except for the last guid.

The solution to this issue

The solution to this issue is to change the regular expression and the matching code a little bit.

The first thing we will do is change the regex to the following:

 
private const string DYNAMIC_KEY_REGEX = @"(_([A-Fa-f0-9]{8}(-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}))"; 

This regex matches for any text in the format “_00000000-0000-0000-0000-000000000000” where the “00000000-0000-0000-0000-000000000000” stands for a guid.

the second change we will do is to the code which is highlighted as the issue. This code changes to the following:

 
            string placeholderKey = args.PlaceholderKey;
            Regex regex = new Regex(DYNAMIC_KEY_REGEX);
            Match match = regex.Match(placeholderKey);
            if (match.Success)
            {
                placeholderKey = regex.Replace(placeholderKey, "");
            }
            else
            {
                return;
            }

this new code removes every occurence of a text matching the regex, so any guid starting with the character “_”. By doing this we would now transform the placeholder key “/body_00000000-0000-0000-0000-000000000000/column1_e33898de-6302-4756-8f0c-5f6c5218e02e”  to “/body/column1” which is what we would want to have to guarantee Sitecores default behaviour.