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.

Template Questions

Looping Over Images In A Specific Folder


Reply

2 Posts   526 Views

Avatar
SilverFox55

30 December 2012 at 2:11am Community Member, 2 Posts

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

10 January 2013 at 8:07pm Forum Moderator, 5511 Posts

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