Jump to:

3402 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Looping Over Images In A Specific Folder

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

Page: 1
Go to End
Author Topic: 458 Views
  • SilverFox55
    Avatar
    Community Member
    2 Posts

    Looping Over Images In A Specific Folder Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Looping Over Images In A Specific Folder Link to this post

    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

    458 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.