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.

General Questions /

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

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

php/arrays in templates


Go to End


3 Posts   1967 Views

Avatar
RonnieH

Community Member, 14 Posts

20 December 2009 at 4:31am

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

Avatar
bummzack

Community Member, 904 Posts

20 December 2009 at 5:14am

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.

Avatar
RonnieH

Community Member, 14 Posts

20 December 2009 at 6:52am

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