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

If function for images in ss template not working


5 Posts   1594 Views

Samba Sam

13 November 2009 at 5:31pm (Last edited: 13 November 2009 5:35pm), Community Member, 85 Posts

I am trying to get the function ImagesR, which checks for occurrence of any one of three images, to work.

This is what I have in the ss template:

<% if ImagesR %>
<img width="$ImageWidthR" src="<% control ImageR.SetWidth(280) %>$URL<% end_control %>" alt="$AltTextR" title="$TitleTextR" />
<img width="$ImageWidthR" src="<% control ImageR2.SetWidth(280) %>$URL<% end_control %>" alt="$AltTextR2" title="$TitleTextR2" />
<img width="$ImageWidthR" src="<% control ImageR3.SetWidth(280) %>$URL<% end_control %>" alt="$AltTextR3" title="$TitleTextR3" />
<div class="caption"><p><i>$CaptionR</i></p></div><% end_if %>
<% end_if %>

This is what I have in Page.php
class Page_Controller extends ContentController {
function ImagesR() {
return ($this->ImageR || $this->ImageR2 || $this->ImageR3 );

It doesn't work. It works if I pass non-image arrays (e.g., $this->CaptionR), but not apparently with images.

Is there a trick for creating an OR function for images (e.g., class Page_Photo extends Image)?

I don't get it?



13 November 2009 at 9:30pm Forum Moderator, 5511 Posts

I am assuming this is because an Image is a relation. So you need to check that the image object exists.

return ($this->Image1() ....

// or the more verbose way..

return (($this->Image1ID && $this->Image1()->exists() || ......


14 November 2009 at 12:48am Community Member, 202 Posts

I tested this and to my amazement this worked: I did create an Image2 in my template, but how should I know that this also includes the creation of an Image2ID? Image2()->ID I can understand. Or should I know this - and is this just me being a N00b?

For the record: I compared what the Image2() returns with or without an Image, and noticed that for instance a Title was present when there actually was an Image2 on a page. Testing for Image2()->Title should therefor enough of an indication that there is an image. (Of course this could change in future releases.)

Therefor I tested with just $this->Image2()->ID and with just Image2()->exists(), and they both give me the right result. Why the double testing? (I would prefer the Image2()->exists())

Samba Sam

14 November 2009 at 3:48am (Last edited: 14 November 2009 3:54am), Community Member, 85 Posts

Thanks guys!
Big help.

So I experimented with the ways suggested so far:

1. return ($this->ImageR() || $this->ImageR2() || $this->ImageR3());

2. return (($this->ImageRID && $this->ImageR()->exists()) || ($this->ImageR2ID && $this->ImageR2()->exists()) || ($this->ImageR3ID && $this->ImageR3()->exists()));

3. return ($this->ImageR()->exists() || $this->ImageR2()->exists() || $this->ImageR3()->exists());

Interestingly versions 2 and 3 worked fine. But version 1 didn't. It was returning a false true on the second image. For example, to test it, I attached only the third image to a page (ImageR3), and removed it from the function: So it was just "return ($this->ImageR() || $this->ImageR2());". Yet the function was still passing true? The image appeared on the page. I then removed $this->ImageR2() from the function as well, so that it was just returning $this->ImageR(). Only then did the ImageR3 on the page not appear.
The short of it is, I am sticking with the 2nd and 3rd options for now.

Thanks again,


14 November 2009 at 4:48am Community Member, 202 Posts

return print_r(ImageR()) always returns something, but only when there is an image in it, it returns data about the image. So you have to check if the image exists.