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

Holder Pages showing limited number of Child Page Elements

Go to End

11 Posts   4224 Views


29 January 2011 at 9:09am (Last edited: 29 January 2011 9:17am), Community Member, 39 Posts

Here is my setup/dilemma

ImageGalleryPageImage [a dataobject - a has many relationship of ImageGalleryPage]

Here is the current state [in development][url][/url]

On the the Image Gallery Holder Page I want to display all of the ImageGalleryPages, *but* in that display only show say 3 images from that ImageGalleryPage. Right now in the holder page i just do <% control Children %> and it iterates and shows thumbs for *all* of Images belonging to a ImageGalleryPage. I would just like call a method that returns only 3 of this Images, or in the template just limit the iteration in the Child to 3.

I have tried multiple ways to create my own method in ImageGalleryHolder which is called once I start iterating, but I think I run into the scope issue of $Top and $Children. Once I get down into control ImageGalleryPageImages I cannot seem to get that Page's ID in a method to then get those files.

Here is the code I have in my

   <% if Children %>
         <ul class="imageGalleries">
         <% control Children %>
               <h3><a href="$Link" title="$GalleryTitle">$GalleryTitle</a></h3>
               <p>{$GalleryExcerpt} <a href="$Link" title="$GalleryTitle">View the complete gallery &raquo;</a></p>
               <% control ImageGalleryPageImages %>
                  <li class="imageGalleryItem">
                     <a rel="imageGallery" title="$Caption" href="$Attachment.Filename">
                        <% control Attachment %>
                         <img src="$CroppedImage(160,160).URL" alt="$Title" />
                        <% end_control %></a>
                     <span class="caption">$Description</span>
               <% end_control %>
         <% end_control %>
      <% end_if %>

** I know there is the Image_Gallery Module from Uncle Cheese, but this requirement is a bit of an edge case. So it not a solutin to just use that module.

Thanks in advance for any help.


29 January 2011 at 9:28am Community Member, 10 Posts

The obvious answer than comes to mind is that you should be able to limit the number of returned entries in a control loop. If you set your ImageGalleryPageImages() method to limit 3, it should only loop 3 times. Alternatively, if you want the flexibility to call that method other places in the site and return a different number of images, you can set the method to accept a parameter for the limit then call it like

<% control ImageGalleryPageImages(5) %>

in the template and you should get 5 items to loop through in this case.


29 January 2011 at 9:47am Community Member, 39 Posts

Hi Matt,

Thanks for your response. I actually was trying to do exactly as you say, but the issue I run into is that the method I call for some reason does not seem to recognise the curent ImageGalleryPage in that iteration.

So if I have that method and place it in the ImageGalleryHolder_Controller I tried the following

   function ImageGalleryPageImages($num = 5 ) {
      return DataObject::get("ImageGalleryPageImage", "`ImageGalleryPageID`" = $this->ID", "SortOrder DESC", "", $num);

This just doesn't return anything, because I am not sure if the method knows what the $this->ID is referring to. I have tried moving this emthod to the ImageGalleryPage and also to the Page.php but have not had any luck. I am hoping I am just missing something really simple. I am thoinking it may be nesting thing in th eiteration as I am getting deeper and looking for Images of teh Page, but I am not really sure.



29 January 2011 at 10:37am Community Member, 10 Posts

If the method you're calling is in the Controller and the controller has the $has_many relation to the images, then you should be able to call

$this->getComponents('HasManyRelationName', '', '', '', $limit);


$this->data()->getComponents('HasManyRelationName', '', '', '', $limit);

if the first one doesn't work.


29 January 2011 at 12:15pm (Last edited: 29 January 2011 12:15pm), Community Member, 39 Posts

Thanks for the tip. Even though it didn't work in this case, I have learnt something new that I didn't know before.

So back to the issue, I tried adding the following to the Holder (and later the Page) but it still did not return anything.

function ImageGalleryPageImage($num = 5 ) {
$results = $this->getComponents('ImageGalleryPageImages', '', '', '', $num);
return $results;

I think it is something do do with the scope of $this in the depth of the control loops, but I have no idea how to reach in and get the $this for the current ImageGalleryPage. I think if I can get the ID or just get at current object then I will be all set, but to get at it is the challange.

Thanks for all the help to date.


29 January 2011 at 12:20pm Community Member, 10 Posts

$this should apply to the current scope in the PHP. It has no bearing on the template. $this refers to the class you're writing code in.

Is that ImageGalleryPageImage() method in the Controller or the Page subclass? Try moving it to the Page subclass if it's in the Controller right now.


29 January 2011 at 3:41pm Community Member, 25 Posts

Sorry if this is rubbish and/or typo ridden but its 2:37am and I'm on my tiny android phone with rubbish cut and paste...

If you put a custom getter in the GalleryPage class, like the get getChildren() function in this thread: -
But replace 'children' with 'GalleryPageImages' that should work.


29 January 2011 at 3:46pm Community Member, 25 Posts

Aram first example of getChildren that is

Go to Top