Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Using $Parent within nested controls to get parent control - not possible?


Reply


2 Posts   1323 Views

Avatar
jumprock

Community Member, 8 Posts

13 December 2011 at 1:44pm

Edited: 13/12/2011 1:45pm

Finally got a chance to use Silverstripe and I’m on top of most things I need but was wondering if I could get some advice on a particular problem I’m having. Google/the forums haven’t been too helpful.

My Basic question is how can I access properties from one step higher in a nested control (in a .ss template). eg:

<% control one %>
<% control two %>
$Parent.MyProperty
<% end_control %>
<% end_control %>

I would have thought that calling $Parent from within control two would refer to control one and I could access the property from there, but it doesn't seem to work.

My specific set up is that I have a parent page, that is using the AllChildren control to display content from all of it child pages. Within each page I am using a control to build an image gallery for which I need to pass a shared unique id to all of it’s images.

The code is structured like this:

<% control AllChildren %>
<table class="gallery_thumbs" cellpadding="0" cellspacing="0">
<tr>
<% control GalleryImages %>

<td><a class="gallery_item_link" href="$Image.URL" rel="gallery_$Parent.Pos" title="$Caption"><div>$Image.CroppedImage(80,55)</div></a></td>

<% end_control %>
</tr>
</table>
<% end_control %>

So on each of the sub pages there is a GalleryImage control which stores the photos for it’s gallery. The lightbox I am using groups images on the page via shared rel tag so I need images from each gallery to have the same rel tag (which is different to the rel tag used for the other galleries), hence I am trying to use the $Pos value for the sub page it’s on, however it doesn’t seem to work,it returns “1” for every page gallery.

I’m not sure if Silverstripe is supposed to be able to do that – I’m suspicious that from what I could tell on the forums the $Parent command is for traversing up the site tree as opposed to nested controls, but I’m eagerly hoping there is a way to do what I’m trying to.

Would be grateful for any advice at all, cheers,

Avatar
Willr

Forum Moderator, 5513 Posts

13 December 2011 at 7:45pm

You can only use $Parent if there is a relationship from the controlled model back to the outer control. $Parent will do a relationship lookup, not a template lookup so thats why $Parent doesn't work (apart from controlling pages -> children).

Some discussion on the addition of a $Up or <% Parent %> function is on the developers mailing list - http://groups.google.com/group/silverstripe-dev/browse_thread/thread/34bcb075be21420a/f3419a2e4f23daf1?lnk=gst&q=%24Up#f3419a2e4f23daf1.

If your case - why don't you put the $Pos on the tr element. Also suggestion, don't use rel for storing metadata. Use data attributes!