Jump to:

3397 Posts in 978 Topics by 854 members

Template Questions

SilverStripe Forums » Template Questions » If function for images in ss template not working

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

Page: 1
Go to End
Author Topic: 1529 Views
  • Samba Sam
    Avatar
    Community Member
    85 Posts

    If function for images in ss template not working Link to this post

    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?

    Sam

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: If function for images in ss template not working Link to this post

    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() || ......

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: If function for images in ss template not working Link to this post

    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
    Avatar
    Community Member
    85 Posts

    Re: If function for images in ss template not working Link to this post

    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,
    Sam

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: If function for images in ss template not working Link to this post

    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.

    1529 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.