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.

Archive /

Our old forums are still available as a read-only archive.

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

Accessing cookie values in template


Go to End


2 Posts   1757 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

14 June 2008 at 10:05pm

Edited: 14/06/2008 10:05pm

Hi, I have a chunk of code in the Page_Controller on a page type called TourPage, that checks a $_GET value in the URL (from a form submission) and uses that value to set a default currency in the Cookie. (Default is set in Page init()). Code as follows:

class Tour_Controller extends Page_Controller {
	function init() {
		parent::init();
		
		// If a currency has been selected, update cookie to keep track of user preference
		$VivaCurrencyCookie = new Cookie;
		if($_GET['viva_cur']) {
			$VivaCurrencyCookie->set('viva_currency', $_GET['viva_cur']);
		}
	}		
}

I've tested this and it works fine. But I am unable to work out how to access that cookie value on the template itself. I want to use the cookie value in if conditions to display the correct price. How can you access (or pass to the main class) a variable calculated in a controller?
Cheers
Aaron

Avatar
Double-A-Ron

Community Member, 607 Posts

14 June 2008 at 10:14pm

Edited: 14/06/2008 10:14pm

Ah, got it - added this function under the init above:

function getCookieCurrency() {
		$VivaCurrencyCookie = new Cookie;
		return $VivaCurrencyCookie->get('viva_currency');
	}