Create a cookie

To create a cookie with the variable name awesomeCookieName and with a value of "James is awesome" use the code below:

You must remember to add an expiry date/length to the cookie. The example below uses 360 days. If you do not set a time, the cookie won't actually store the data past the session (you may wish to use this feature).

  1. $cookietool.add("awesomeCookieName","James is awesome",360)  

You can add a cookie to the header of the page (but just one cookie addition per .vtl) using the following method. By using this method you can set a more explicit expiry date and you can also set the path of the cookie. If you need to store values with single quotes, special characters, including semi-colons etc then you will need to place the value inside quotes. You will need to use the following escape: ${esc.q}$value${esc.q}

  1. $response.setHeader("Set-Cookie","showContentCookie=$showContent;expires=Sat, 01-Jan-2093 00:00:00 GMT;path=/")  

Delete a cookie

You can delete a cookie like so:

  1. $cookietool.add("awesomeCookieName","",0)  

Retrieve cookie value

To retrieve the value of a cookie use the following code:

  1. #set ($cookieValue = $cookietool.get("awesomeCookieName").value)   

If retrieving a value from the header then you can use the following (just remove get):

  1. #set ($cookieValue = $cookietool.awesomeCookieName.value)   

See more information on creating cookies here:

Create and display a cookie with multiple values using XML

If you need to add a cookie containing more information you'll need a method of parsing the data. I used XML on the home page on the filters and an abridged version can be seen below (or check vtls on home page):

  1. ##This builds a query item for region  
  2. #if($request.getParameterValues('regions'))  
  4.     #foreach ($regions in $request.getParameterValues('regions'))  
  5.         #if ($query_regions)  
  6.             #set ($query_regions = "$!{query_regions} newsItem.regions:*${regions}*")  
  7.             #set ($xml_regions = "$!{xml_regions} <REGION>${regions}</REGION>")  
  8.         #else  
  9.             #set ($query_regions = "newsItem.regions:*${regions}*")  
  10.             #set ($xml_regions = "<REGION>${regions}</REGION>")  
  11.         #end  
  12.     #end  
  14.     #if ($query_regions)  
  15.         #set ($query_regions = "+(${query_regions})")  
  16.     #end  
  18. #end  
  20. ##You can also add single values like so:  
  21. #set ($filterName = "<FILTERNAME>APPLE</FILTERNAME>")  
  23. ##Creates the XML format and adds the region from above  
  24. #set ($myXML = "<?xml version='1.0' encoding='ISO-8859-1'?><COOKIE><ITEM>$!{xml_regions}$!{filterName}</ITEM></COOKIE>")  
  26. ##Adds the XML above to a cookie  
  27. ##$cookietool.add("CustomQueryCookie","$myXML")  
  28. $response.setHeader("Set-Cookie","CustomQueryCookie=${esc.q}${myXML}${esc.q};expires=Sat, 01-Jan-2093 00:00:00 GMT;path=/;")  
  30. ##Gets the XML from the cookie  
  31. #set ($xml = $cookietool.get("CustomQueryCookie").value)  
  32. #set ($myXML = $xmltool.parse($xml))  
  34. ##Displays the regions stored in the XML  
  35. #foreach($item in $myXML.children().iterator())  
  37.     #set($cdXML = $xmltool.parse($item))  
  38.     $cdXML.TOPIC<br>  
  39.     $cdXML.TOPIC.text  
  41. #end  
comments powered by Disqus