MMBase tips

Baking cookies (or using sessions)

tip #9 / Tue 26 Jan 2010 / André / 0 comments

The mmbase taglib has some convenient ways to bake your own cookies. Creating session variables is done in more or less the same way.

In my_editors I use the following piece of code to look for users cookies in case they have preferences that are different from the default settings. The defaults '25', 'top' and '99' for 'max', 'searchbox' and 'maxdays' are used if there are no cookies.

<mm:import id="max" from="cookie" externid="my_editors_maxitems">25</mm:import>
<mm:import id="searchbox" from="cookie" externid="my_editors_searchbox">top</mm:import>
<mm:import id="maxdays" from="cookie" externid="my_editors_maxdays">99</mm:import>

Users can save their preferences by submitting a form. Their settings are imported from the parameters 'max_items', 'search_box' and 'max_days' and written as cookies 'my_editors_maxitems', 'my_editors_searchbox' and 'my_editors_maxdays' in their browsers cookie jar.

<%-- read defaults or configuration submitted by form --%>
<mm:import externid="max_items" vartype="Integer"><mm:write referid="max" /></mm:import>
<mm:import externid="search_box"><mm:write referid="searchbox" /></mm:import>
<mm:import externid="max_days" vartype="Integer"><mm:write referid="days" /></mm:import>

<mm:import externid="savethis" />
<mm:present referid="savethis">
<%-- write the cookies --%>
<mm:write cookie="my_editors_maxitems" referid="max_items" />
<mm:write cookie="my_editors_searchbox" referid="search_box" />
<mm:write cookie="my_editors_maxdays" referid="max_days" />

Sessions are done the same way. Write a value to the session with <mm:write /> using the attribute 'session'.

<mm:write session="your_session_value" value="some value..." />

And pick it up in a different location with <mm:import /> and specify with the attribute 'from' that you need it from session.

<mm:import from="session" id="your_session_value" />

When you print it you get 'some value...'.


RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase Taglib reference
The reference for all tags mm