Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » php/arrays in templates

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1293 Views
  • RonnieH
    Avatar
    Community Member
    14 Posts

    php/arrays in templates Link to this post

    Hi guys,

    I suspect my problem should be simple enough to solve, but thus far I have not been able to find a solution through trial and error, nor through searching these forums (perhaps I've used the wrong keywords). In addition, I'm not that experienced with scripting and whatnot.

    What I want to do is display three pictures from a folder at random as part of my general template. I have tried using php in my template, but it gave me loads of errors so I concluded that silverstripe doesn't like php in its .ss files. Therefore I've tried to move this php to the page_controller, this has worked to some degree, however the problem is that I have to call the random-function three separate times (one time for every picture) and I have no way of preventing duplicate pictures resulting from the function-calls.

    What I have done is given the pictures class-labels which can vary from p1 to p12, each one having a certain img-url associated to it through the css. In the .ss template, one of the images will look like this:

    <div id="photo1" class="$RandomImage(1)"></div>

    In the page_controller I have made the following function:

       public function RandomImage($n) {
          $photos = array("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10","p11","p12");
          shuffle($photos);
          switch ($n) {
          case 1:
             return $photos[0];
             break;
          case 2:
             return $photos[2];
             break;
          case 3:
             return $photos[4];
             break;
          }
    }

    (For some reason, it will always yield three duplicate images if I don't use this switch-construction, I guess it has something to do with caching...)

    What would seem the most logical solution to me, would be to pass an array of three values to the template, and use these to determine which pictures display, but again silverstripe doesn't seem to appreciate me trying to use PHP in my template-files to read what's in the arrays.

    Anyone have any thoughts?

    Thank you in advance for your time/trouble.

    Kind regards,
    Ronnie

  • banal
    Avatar
    Community Member
    901 Posts

    Re: php/arrays in templates Link to this post

    I think the problem here is that you're creating and shuffling the array on every function call. This might result in duplicates.. yes.
    I suggest you shuffle the array upon initialization, then simply use the RandomImage function as accessor. Something like:

    protected $photos;

    public function init(){
       parent::init();
       $this->photos = array("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10","p11","p12");
       shuffle($this->photos);
    }

    public function RandomImage($n) {
       return $this->photos[(int)$n];
    }

    This way the array is being created and shuffled when the page is initialized. The RandomImage function just serves as accessor to the array elements and doesn't shuffle the array anew.

  • RonnieH
    Avatar
    Community Member
    14 Posts

    Re: php/arrays in templates Link to this post

    Dear banal,

    Thank you very much for your quick and comprehensive assistance, my problem appears to have been solved!

    Have a nice day!

    Kind regards,
    Ronnie

    1293 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.