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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Looping Over Images In A Specific Folder


Go to End


2 Posts   1640 Views

Avatar
SilverFox55

Community Member, 2 Posts

30 December 2012 at 2:11am

I have been over numerous portions of the documentation and still cannot find an answer to this specific question:

I have one page template which will be used for multiple (different) subjects with corresponding (by subject) images. Each subject will have its own nested page. I want to organize the images into different folders by subject (Images/Subject1, Images/Subject2, etc.) , and then display the individuals images in the nested page by looping over them in their particular folder. How and where would I specify the particular folder to loop from, as I am also guessing this will be based on which nested page is currently active.

Question: How exactly would this loop be written and how would the particular folder and path be specified based on the "current" nested page?

Many thanks in advance!

Avatar
Willr

Forum Moderator, 5523 Posts

10 January 2013 at 8:07pm

You may want to do all 5 tutorials if you haven't done them yet as they'll contain the tools you need to achieve this.

First you create your page type for your custom page, on this page type you set up a has_one relationship to a 'folder' and add a TreeDropdownField to allow you to select the folder in the CMS.

class MyPageType extends Page {

public static $has_one = array(
'ImagesFolder' => 'Folder'
);

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTable('Root.Content.Main', new TreeDropdownField('ImagesFolderID'));
}
}

Then after running dev/build you can select the folder in the CMS backend. To select the images from that folder you can add another function to that class

function getUploadedImages() {
return ($this->ImagesFolderID) ? DataObject::get('Image', "ParentID = '$this->ImagesFolderID'") : false;
}

Then use <% control UploadedImages %>... <% end_control %> to loop the images.

* Note you may run into syntax errors