7

I'm using dynamic placeholders in the views, and there are nested views.
When giving the placeholder path for a rendering (in Presentation > Details),
the path should be - main-placeholder/sub-placeholder. But it doesn't render anything in that way. When given only the sub-placeholder, it works.

Using Sitecore 8.2 MVC

Here are my class and views:

HTMLHelperExtensions.cs:

public static class HTMLHelperExtensions
    {
        public static HtmlString DynamicPlaceholder(this SitecoreHelper mySCHelper, string placeholderName)
        {
            string dynamicKey = GetDynamicKey(placeholderName);

            if (!string.IsNullOrWhiteSpace(dynamicKey))
                placeholderName = dynamicKey;

            IDisposable disposable = PlaceholderContext.Enter(new PlaceholderContext(placeholderName));
            return mySCHelper.Placeholder(placeholderName);
        }

        private static string GetDynamicKey(string placeHolderName)
        {
            bool needIncrement = false;
            int incrementStep = 0;
            IEnumerable<PlaceholderContext> myPlaceholders = ContextService.Get().GetInstances<PlaceholderContext>();

            foreach (PlaceholderContext myPHContext in myPlaceholders)
            {
                if (myPHContext.PlaceholderName == placeHolderName || myPHContext.PlaceholderName.StartsWith(placeHolderName + "_"))
                {
                    needIncrement = true;
                    incrementStep++;
                }
            }

            if (needIncrement)
                placeHolderName += "_" + incrementStep.ToString();

            return placeHolderName;
        }

    }

default.cshtml

<body>
 @Html.Sitecore.Placeholder("page-content")
</body>

Section Centered.cshtml

<section>
    <div class="container">
        @Html.Sitecore().DynamicPlaceholder("section-centered")
    </div>
</section>

2 Column 6-6.cshtml

//this should come inside "section-centered"
<div class="row">
    <div class="col-lg-6 col-md-6 col-sm-6">
        @Html.Sitecore().DynamicPlaceholder("col-wide-1")
    </div>
    <div class="col-lg-6 col-md-6 col-sm-6">
        @Html.Sitecore().DynamicPlaceholder("col-wide-2")
    </div>
</div>

The content is rendered, when I give the placeholders as this:
enter image description here

When I give the placeholders like this:
enter image description here

it only renders...

<section>
    <div class="container">
        ::before
        ::after
    </div>
</section>

As you see, the Article is not rendered. I would like to give the nested paths as it would make sense. Where am i going wrong.

5
  • 2
    at first glance your Dynamic Key generation logic is incorrect. This will leave your Experience editor messy. Also, in presentation details the path shown is after trimming it. there are few marketplace modules and github code for generating right key and have proper path displayed in presentation details.
    – phani
    Jan 13, 2017 at 7:57
  • 1
    Assuming that you're using a fully featured dynamic placeholder solution, have you tried adding components using the Experience Editor, rather than setting placeholder names manually in Presentation Details? Use the EE and see whether that works, and what placeholder names are generated. Jan 18, 2017 at 12:25
  • 1
    Please attach the source code of PlaceholderContext. Jan 18, 2017 at 12:57
  • Could you attach the source to your patch config for any pipeline overrides you've implemented. Typically with dynamic placeholder logic there is two parts (maybe even three if you extend the chrome data rendering). Jan 19, 2017 at 15:19
  • Why manually "path" your placeholder assignments when you've just written code to ensure that your DynamicPlaceholders all have unique IDs? Jan 20, 2017 at 16:08

2 Answers 2

5
+25

It's not really an answer to your direct question, but it's a solution. I am using Fortis dynamic placeholder for MVC right now. Works out of the box. Zero problems with nesting.

Site: http://fortis.ws/fortis-collection/dynamic-placeholders/

NuGet: https://www.nuget.org/packages/DynamicPlaceholders.Mvc/

<div class="container-fluid">
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
            <div class="box-row">
                @Html.Sitecore().DynamicPlaceholder(Bonfire.Feature.Grid.Constants.Strings.ColNarrow1)
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-9 col-lg-9">
            <div class="box-row">
                @Html.Sitecore().DynamicPlaceholder(Bonfire.Feature.Grid.Constants.Strings.ColWide1)
            </div>
        </div>
    </div>
</div>

showing placeholder

1
  • this is quite valid. i dont see a reason why one should re-invent them Jan 20, 2017 at 9:51
1

Try this:

public static HtmlString DynamicPlaceholder(this Sitecore.Mvc.Helpers.SitecoreHelper helper, string dynamicKey, char identifier)
        {
            try
            {
                var currentRenderingId = RenderingContext.Current.Rendering.UniqueId;
                string key = string.Format("{0}|{1}_{2}", dynamicKey, identifier, currentRenderingId);
                return helper.Placeholder(key);
            }
            catch (Exception ex)
            {
                Log.Error("SitecoreHelper - DynamicPlaceholder: Exception Ocurred " + ex.Message + ex.StackTrace, ex);
                if (ex.InnerException != null)
                {
                    Log.Error("SitecoreHelper - DynamicPlaceholder: Inner Exception: " + ex.InnerException.Message+ ex.InnerException.StackTrace, ex.InnerException);
                }
            }
            return helper.Placeholder(Guid.NewGuid().ToString());
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.