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

Read folder


Go to End


2 Posts   1461 Views

Avatar
silverprogramer

Community Member, 1 Post

4 February 2014 at 11:44am

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

Community Member, 344 Posts

6 February 2014 at 10:52pm

Edited: 06/02/2014 11:54pm

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 %>