Search code examples
expressionengine

Getting cookie values and use it in ExpressionEngine Templates


I would like to use some stored cookie values in my ExpressionEngine templates without using any PHP.

My current template code

{exp:channel:entries channel="favorites" category="<?php echo $COOKIE['favorite']?>"}
{title}
{/exp:channel:entries}

This is ugly and doesn't perform that well under load. Any suggestions? Maybe an addon?


Solution

  • You have to set cookies in EE with the set_cookie() method in the Functions class.

    http://expressionengine.com/user_guide/development/reference/functions.html#set-cookie-name-string-value-string-expire-int

    $this->EE->functions->set_cookie('your_cookie_name', 'some_value', 3600 * 24) // Set a cookie that expires in 1 day
    
    $this->EE->input->cookie('your_cookie_name'); // returns 'some_value';
    

    EE Add-on from Devot:ee

    http://devot-ee.com/add-ons/cookies