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