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

How to call in just the first image for a Page that is associated via $many_many?


Go to End


4 Posts   1608 Views

Avatar
Turismo

Community Member, 28 Posts

23 May 2016 at 8:49pm

Hi,

I'm trying to figure out how to call in the first image for a page with one of my template .ss files. The images are associated with the page via $many_many.

Here is mysite/code/CasePage.php:

class CasePage extends Page {

	 private static $db = array (
        'Intro' => 'Text'
    );

	private static $many_many = array (
        'Photos' => 'Image'
    );

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Main', TextareaField::create('Intro','The introduction for the page'),'Content');

		$photos = new UploadField('Photos', 'Photos for this case study', $this->Photos());
		$fields->addFieldToTab('Root.Photos', $photos);

		$photos
	        ->setFolderName('case-study-photos')
	        ->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png'));

		return $fields;
	}

}

class CasePage_Controller extends Page_Controller {

}

And I've got a loop for the photos in my .ss file (see below), but I actually just want to call in the first image, and have a SetWidth(550) applied to it. The below calls in all the images, but doesn't work if I change $URL to $URL.SetWidth(550). This is for a holder page that lists a load of case studies, and so for each case study on this page, I'm just wanting to show the first image from a bunch that the client will associate with that case study.

Please help! :)

<% if $Photos %>
<figure class="media">
	<% loop $Photos %>
		<img src="$URL" alt="$Title" title="$Title" width="$Width" height="$Height">
	<% end_loop %>
</figure>
<% end_if %>

Avatar
Turismo

Community Member, 28 Posts

23 May 2016 at 9:34pm

Managed to figure out how to call in the first image in the loop using $first (see below), but I still can't figure out how to apply SetWidth(550) - any attempts seem to break the image:

<% if $Photos %>
	<figure class="media">
		<% loop $Photos %>
			<% if $First %>
				<img src="$URL" alt="$Title" title="$Title" width="$Width" height="$Height">
			<% end_if %>
		<% end_loop %>
	</figure>
<% end_if %>

Avatar
dhensby

Community Member, 253 Posts

24 May 2016 at 2:57am

This should work.

<% if $Photos %>
	<figure class="media">
		<% with $Photos.First %>
				<img src="$SetWidth(550).URL" alt="$Title" title="$Title" width="550">
		<% end_with %>
	</figure>
<% end_if %>

Avatar
Turismo

Community Member, 28 Posts

24 May 2016 at 3:02am

Thanks Dan, that works!