2

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?

2 Answers 2

3

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
  • 1
    Brilliant! We converted this logic to powershell and it works beautifully :)
    – jdylanmc
    Oct 25, 2018 at 19:09
2

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).

2
  • 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, 2018 at 18:44
  • True. Hence the "sort of". I can't think of any 100% reliable way to actually do what OP asks. Oct 24, 2018 at 19:06

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.