Let's assume I have multiple renderings on a page, which are nested within each other- think "outer wrapper", with a "two column" inside, with a "button" inside of that. If I remove a parent rendering (like "outer wrapper"), all child renderings disappear visually. These renderings are still within presentation details, though.

When looping the renderings from within presentation details, how can I detect if a particular rendering (like the button above) is going to be visible on the screen vs. being orphaned off?

up vote 3 down vote accepted

If all of your dynamic placeholder keys contains the rendering UID then it should be possible. Let's expect that your placeholder keys look like this:

/main/wrapper_0b720b64f1a648dd9774cdc1484e7152

The Guid is the UID of the Wrapper rendering item. In this case the following method could work:

private bool IsRenderingShown(RenderingReference rendering, RenderingReference[] renderings)
{
    if (rendering == null || string.IsNullOrEmpty(rendering.Placeholder) || renderings == null)
    {
        return false;
    }

    var partPlaceholders = rendering.Placeholder.Split('/', '_');
    foreach (var p in partPlaceholders)
    {
        if (!Guid.TryParse(p, out Guid uniqueId))
        {
            continue;
        }

        var renderingFound = renderings.Any(r => Guid.TryParse(r.UniqueId, out Guid u) ? u.Equals(uniqueId) : false);
        if (!renderingFound)
        {
            return renderingFound;
        }
    }

    return true;
}
  • 1
    Brilliant! We converted this logic to powershell and it works beautifully :) – Dylan McCurry Oct 25 at 19:09

At run-time, yes. Sort of.

But if you're considering a programmatic loop, there are various reasons why this would be very difficult to achieve.

Thing is: Sitecore doesn't know which placeholders will be emitted on any given Layout/Page. These placeholders are emitted by the components on the page at the time they are rendered, and Sitecore then loops through the presentation details again for "dives deeper".

So the only way (I can think of) to achieve this, would be to run the rendering pipeline for the Item in question and then collect the placeholder information. Would be a pretty expensive call and might cause all sorts of problems (rendering outside an actual HttpRequest context).

  • 1
    Correct me if I'm wrong but this approach would only work if the page was always rendered in the same way. If any part of the page is dynamically shown or hidden based some code or on Sitecore personalization rules, there is no way to detect in runtime if certain component will be used if some specific conditions are met. – Marek Musielak Oct 24 at 18:44
  • True. Hence the "sort of". I can't think of any 100% reliable way to actually do what OP asks. – Mark Cassidy Oct 24 at 19:06

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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