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

Read folder


Reply

2 Posts   314 Views

Avatar
silverprogramer

4 February 2014 at 11:44am Community Member, 1 Post

HI im starting with Silverstripe, some one can help me?

Hi im trying to read some directory to do my carousel

my problem here is: how can i access from my values on a loop

Template:

<% loop getSliderFiles %>

#HOW I CAN READ THE NAME OF MY FILES FROM HERE?

<% end_loop %>

Controller php:

public function getSliderFiles()
{
$list = new ArrayList();
foreach (scandir(Director::baseFolder().'/assets/slider/' ) as $file_die)
{
$list->push($file_die);
}

return $list;
}

any help?

Avatar
Devlin

6 February 2014 at 10:52pm (Last edited: 6 February 2014 11:54pm), Community Member, 215 Posts

It's quite simple.

public function getSliderFiles() {
   $folder = Folder::find_or_make('slider');
   return $folder->Children();
}

<% loop getSliderFiles %>
$Filename
<% end_loop %>

---
Edit: Or if you're fancy, you can define the folder as a has_one relation to the DataObject. e.g.:

class Page extends SiteTree{
   private $has_one = array(
      'Slider' => 'Folder'
   );
}

<% if SliderID %>
<% loop Slider.Children %>
$Filename
<% end_loop %>
<% end_if %>