Hi,
I have rewritten Eric Snyder’s Cookies plugin. The result is Cookie Plus plugin.
I was dissatisfied with two aspects of Cookies plugin:
1) using it you should place the code for setting cookie on top of the page and cookie must be set before any output started;
2) it is not possible to use retrieved cookie as parameter of other tags.
Cookie Plus plugin has no such constrictions and retains all functionality of Cookies plugin.
Usage:
{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600"}
Sets a cookie named mycookie with the value “hellow!”. The cookie
will last for 1 hour (3600 seconds).
Set the seconds to zero to set a session cookie.
By default cookie is set using PHP function. This means that {exp:cookie_plus:set}
tag must be on top of the page and do its job before any other output. If you need
to set a cookie after output started, you may use parameter “method”.
In case parameter “method” has a value “script” as here
{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script"}
the cookie will be set using client-side javascript.
Using client-side javascript cookie by default will be set during page load event.
Also cookie will be set during page load event if “event” parameter is set to “load”.
To set cookie during page unload event, parameter “event” should have the value “unload”:
{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script" event="unload"}
To retrieve a cookie:
{exp:cookie_plus:get name="mycookie"}
If the cookie “mycookie” exists then it will return the value.
If the cookie does not exist then it will return nothing.
Alternatively for retrieving a cookie you may use {cookie} variable
placed between {exp:cookie_plus:get} and {/exp:cookie_plus:get}
tag pair.
Variable {cookie} within {exp:cookie_plus:get} and {/exp:cookie_plus:get} tag pair is
more powerful because you can use its output as parameter of other tag. For such use
you must add parse=“inward” parameter to {exp:cookie_plus:get} tag.
For example, code as this will work
{exp:cookie_plus:get name="mycookie" parse="inward"}
{exp:weblog:entries weblog="{cookie}" show="1|3|5"}
Some code
{/exp:weblog:entries}
{/exp:cookie_plus:get}
Also you can use retrieved cookie value in conditionals:
{exp:cookie_plus:get name="mycookie"}
{if cookie=="news"}
Some code
{/if}
{/exp:cookie_plus:get}
Changelog:
-1.2 // Added possibility to use retrieved cookie value in conditionals.
-1.1 // Parameter “event” added.
-1.0 // First release.
