Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Using $Parent within nested controls to get parent control - not possible?

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

Page: 1
Go to End
Author Topic: 1255 Views
  • jumprock
    Avatar
    Community Member
    8 Posts

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

    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,

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Using $Parent within nested controls to get parent control - not possible? Link to this post

    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!

    1255 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.