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.

Customising the CMS /

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

Looping out images in template [SOLVED]


Go to End


4 Posts   3900 Views

Avatar
jocken

Community Member, 3 Posts

19 July 2012 at 4:59am

Edited: 19/07/2012 5:00am

I'm struggling hard with SilverStripe 3.0 because the documentation doesn't exist yet.

I wan't to attach multiple images to a Page. It was extremely easy to add the cms-part but I can't loop it out.

Page.php

<?php

class SlidePage extends SiteTree {

	public static $db = array(
	);

	public static $has_many = array(
		'Photo' => 'Image'
	);
		
	public function getCMSFields() {
        $fields = parent::getCMSFields();
         
        $fields->addFieldToTab("Root.Images", new UploadField('Photo'));
         
        return $fields;
    }	

}
class SlidePage_Controller extends ContentController {

	public static $allowed_actions = array (
	);

	public function init() {
		parent::init();

	}

}
?>

This gives me the ability to attach images to a slidepage but I don't know how to loop it out. I really like the simplicity to add and remove images so I don't want to change anything there. Just need help with showing it.

Avatar
colymba

Community Member, 26 Posts

19 July 2012 at 7:19am

what about something like this:

<% if Photo %>
    <% loop Photo %>
        <img src="$URL" alt="$Title" />
    <% end_loop %>
<% end_if %>

Avatar
jocken

Community Member, 3 Posts

19 July 2012 at 8:11am

Seriously? It was that easy? There should be some documentation about this. Anyways, BIG thank you. Worked like a charm!

Avatar
sebastiankozub

Community Member, 59 Posts

24 September 2015 at 8:01am