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.

Template Questions /

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

Random Function not working ss File


Go to End


1796 Views

Avatar
ramu

Community Member, 15 Posts

15 November 2009 at 2:14am

Hi

Please find the my query below. i am using to get random metakeywords from the page

Myfiles:

/code/Page.php
/code/Homepage.php
themes/<theme>/Page.ss <- my Layout template
themes/<theme>/Layout/Homepage.ss <- my template

MetaKeywords: "youtube clone, video scripting, video software

Page.php

...
class Page_Controller extends ContentController {
	function RandomMetaKeywords()
	{
		if ($this->MetaKeywords)
			$HomePageValue = $this;
		else	
			$HomePageValue = DataObject::get_one('HomePage', '', '', '', '');
			
		$keywords = explode(', ',$HomePageValue->MetaKeywords);
		$rand_keys = array_rand($keywords,1);
		$retKey = $keywords[$rand_keys];
		unset($keywords); 
		return ($retKey);
	}
}

Page.ss

...
<div>$Title</div>
$RandomMetaKeywords
$RandomMetaKeywords

<div>$Layout</div>

HomePage.ss

...
<div>$Title</div>
$RandomMetaKeywords
$RandomMetaKeywords

above my code, In Page.ss file successfully get the random values while function call but in layout file ie (HomePage.ss) it will get same value for all the function call.

output of Page.ss

youtube clone
video scripting

output of HomePage.ss

youtube clone
youtube clone

Please help to clear this issue.

Regards,
thisIsRam