How to change a control's Placeholder by device?
Asked Answered
W

1

2

Using Sitecore 7 - Is there a way I can specify what the placeholder of a template's control will be based on device?

Example : Placeholder A if the device is Mobile else Placeholder B

This is more to do with positioning the content differently in Mobile than desktop site.

UPDATE

Trayek's answer is great and would sure work. Although, I ended up using a different approach.

In the layout details of my template, I added a control C; set its placeholder as A and added HideBydevice Desktop in the additional Parameters section. Then, I again added control C; set its placeholder as B and added HideBydevice Mobile in the additional Parameters section.

Then in my code, I check HideBydevice parameter's value and hide the sublayout/control, if the parameter's value is the same as Sitecore.Context.Device.Name

This worked for me.

Whitherward answered 18/9, 2013 at 7:14 Comment(0)
L
6

What you can probably do is use Sitecore's Rules Engine, where you can create (or use a pre-existing) Condition to find out whether you are on a mobile device (to do this, you could use the Mobile Device Detector for instance - although I don't know if that module is supported on Sitecore 7).
Then, you can also use the Action Set placeholder to value.

You could also create your own Action instead, of course. All you'd have to do is get the RenderingReference and simply change the placeholder like so:

var reference = new RenderingReference(this.RenderingID);
reference.Placeholder = "A";

More on this in the Rules Engine Cookbook

Update
I've written a blog post about how to get this done: Read it here.

Leander answered 18/9, 2013 at 8:1 Comment(6)
Thanks, Trayek. That was exactly what I was looking at. I created a conditional rendering Global Rule. Now, apparently I can configure conditional rendering in layout details of standard values for the data template. But, the only option I see is to personalize the relevant control. And there is no option to insert a rule in the Personalization section. Am I missing something here?Whitherward
If you've added it to '/sitecore/system/settings/rules/conditional renderings/global rules' that rule will automatically run for each component. Your Conditional Rendering Rule item you created has a Rule field, which you can use. Just click Edit Rule, select one (or more) condition(s) and one (or more) action(s). I.e. 'where the device is mobile' (default rule from the Mobile Device Detector module) with the action 'set placeholder to B'. You might want to include a custom conditino to detect which placeholder it's targetting to begin with, otherwise all will end up there :-)Leander
Somya, if this answer was or led you to the solution to your question then you should accept it, as it might help others in the community, as well. :)Kashmiri
I've updated my answer with a link to my blog, which will give an example of how to do it exactly.Leander
Yes, Zachary. I was just waiting to implement this and get it working, before I mark it as answer :)Whitherward
Thanks, Trayek for a great blog post :) I actually ended up using a different approach to solve this - but that is, of course, before I read your blog post. I have updated my question with the approach I took. Marked your solution as answer anyway. Thanks again!Whitherward

© 2022 - 2024 — McMap. All rights reserved.