0
votes

I have a rendering but wanted to place it in different placeholders on a page. I wanted to modify the HTML based on the placeholder for the current rendering being processed. Did anyone came across this scenario and solved it? Please share.

Example: I have image carousel rendering item. It has it's own .cs class where the HTML is created. Now, in Sitecore content item's presentation details, I add one image carousel rendering with the placeholder "zone1", and another image carousel rendering to "zone2" placeholder. Based on zone1 and zone2 the HTML varies. I am overriding RenderModel(HtmlTextWriter writer) to override the HTML.

2
Is you question getting the current placeholder? In a user control? it should be this: (Placeholder) this.Parent.Parent;Jan Bluemink

2 Answers

0
votes

Instead of hardcoding this based on the placeholder, you could use rendering parameters to achieve the same. Best approach is to create a list of values your users can select from and put that on a rendering template. Add this to your rendering definition and your users will be able to select one of the values - which could be seen as 'variants' of your control. In your code you can easily get this value and adjust as wanted.

This method will demand some work from your editors as they will need to set the value (you could use standard values to help), but will give you flexibility. If you need the component in new placeholders or any other changes are needed, this flexibility will gain you a lot of time and prevent hard-coded spaghetti.

0
votes

You can get the placeholder from sublayout using the following code:

        Sublayout currentSublayout = this.Parent as Sublayout;

        Placeholder currentPlaceholder = currentSublayout.Parent as Placeholder;

to get the name of the placeholder use:

currentPlaceholder.Key