Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Accessing cookie values in template

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

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

Page: 1
Go to End
Author Topic: 1196 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Accessing cookie values in template Link to this post

    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

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Accessing cookie values in template Link to this post

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

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

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