Accessing child placeholder item(s) from parent rendering

745 views
Skip to first unread message

Kyle Heon

unread,
May 6, 2014, 3:30:48 PM5/6/14
to glasssite...@googlegroups.com
This isn't technically a Glass specific question but I'm hoping someone might be able to provide me with a solution, Glass or otherwise.

I have the following setup:

Promotion Module View Rendering:

<div class="promo-module" style="background-image: url('/~/media/images/cards/promotions/background.png');">
    @Html.Sitecore().DynamicPlaceholder(Model.ItemsPlaceHolder)
</div>

Promotion Card View Rendering:

 <div class="promo-card">
    ...
</div>

The flow for this is that users add a promotion module to a page and then add a promotion card to the placeholder within the module rendering. The module rendering then needs to extract the background from the promotion card (which is an item stored in the child placeholder).

How do I access these items from the parent module view rendering?

I realize that another solution for this would be to set the item AT the module rendering itself but given the way we have our modules/cards configured that isn't as simple as that and I'd prefer to not rework things to support this that way right now unless absolutely necessary.

Thanks in advance!

-K

Pavel Veller

unread,
May 7, 2014, 9:38:19 AM5/7/14
to glasssite...@googlegroups.com
Hi Kyle,

I have two thoughts:

1) All renderings that you bind to placeholders (dynamic or not) end up in the Layout Details (__Rendering) of the item. If you need to get a hold of the item that is set as a datasource on one of those renderings the best bet is probably to read that @ds attribute from the <r /> element. I once blogged about various forms of layout details - http://jockstothecore.com/solid-liquid-gas-three-states-of-layout-details. You won't find the parsed state (LayoutDefinition) within the MVC pipelines - they pass around different object from Sitecore.Mvc.Presentation namespace but once you start looking you will find it (something like Sitecore.Mvc.Presentation.PageContext.Current.PageDefinition.Renderings). You should be able to find your card rendering in there inspecting what placeholder (@ph) it is attached to.

2) If the item(s) you need are not on the promotion card rendering datasource and are, for example, passed into that rendering as a view model object then you are probably less lucky. When the "outer" promotion model rendering runs the "inner" promotion card rendering isn't there yet. The <mvc.renderRendering> will ultimately delegate to ASP.NET  via PartialExtensions.Partial() and only come back to Sitecore when it executes your razor view and that Placeholder() extension method that will call  <mvc.renderPlaceholder>.  It's like a recursion and you can actually see it if you Debug your page in the Page Editor. So I guess what I am saying is - you should be able to pass something from the model rendering to the card rendering or "return" something from the card rendering back to the model rendering but you can't, not without some elaborate hackery, pull something from the card rendering before it runs. I hope I am making sense here. 

I hope it helps

--
Pavel
Reply all
Reply to author
Forward
0 new messages