Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore: know the placeholder the current sublayout is in?

Tags:

sitecore

Is there a way to know which placeholder a sublayout is sitting in through code?

Scenario: I would like for a sublayout to appear a little different if it is in Placeholder B as opposed to Placeholder A. I could do this with sublayout parameters but I am trying to make it so an author doesn't have to set up a value and rather the sublayout is just smart enough to know what context it is being used in.

like image 515
Colema18 Avatar asked Nov 12 '13 19:11

Colema18


2 Answers

You can probably do something clever with the Rules Engine, and this is probably how I would tackle this - possible change a rendering parameter using the Rules Engine based on the Placeholder and then do something in your code based on that...

But you can doing the following from code:

Sublayout sublayout = this.Parent as Sublayout;
if (sublayout != null)
{
    Placeholder placeholder = sublayout.Parent as Placeholder;
    if (placeholder != null)
    {
        // name of the container placeholder
        string placeHolderKey = placeholder.Key;
        // full path of nested placeholders
        string placeHolderContextKey = placeholder.ContextKey;
    }
}
like image 88
jammykam Avatar answered Oct 02 '22 10:10

jammykam


As @jammykam says I would also encourage you to use Rules Engine to achieve a solution. And for that I will point you in the direction of this answer on Stackoverflow for a similar question(I think)

This should give you a good insight into the similar sort of scenario which the other user was facing.

Finally would encourage you to read Trayek's blog post on Changing placeholders with Rules

Hope the above helps and get you the solution that you are after.

like image 35
Shriroop Avatar answered Oct 02 '22 10:10

Shriroop