Cookies

Cookies

{{ cookies }}

The cookies object is available on every page, and contains information regarding the cookies sent with the request. Note that this will typically only include cookies for the current or top-level domain on multi-domain sites.

Properties of {{ cookies }} objects
Name Type Description
is_valid Boolean Will always be true
keys list The list of cookie names on the current request, including cookies that may have been added or removed after the request started processing.
* String Individual cookies on this request may be accessed using {{ cookies.cookieName }} or {{ cookies['cookie-name'] }} syntax
output String JSON representation of the cookies object, similar to {{ cookies | inspect: 3, false }}

The cookies object is copyable, and when copied using the {% copy_to_dictionary %} the keys will be the names of the cookies and the values will be the corresponding cookie objects. You may also treat this object as a list containing all of the cookie names which may be iterated using a {% for %} loop.

Related

Root Scope Reserved Variables

Every page on the site is created by processing templates using specific pre-defined inputs. These reserved variables are present on the root scope of every pageload.

{{ client }}

The client object is available on every page, and contains information about the history of the browser used to access the site. Note that most of these properties are only meaningful if the user has allowed permission for sessions. Additionally, client properties require cookies in order to work. Requests made without cookies (such as by bots or browsers with cookies disabled) or without permission for session will always behave like an initial page-load without existing client information. The client object is one of a handful of simple mechanisms to enable personalization on your site. Used well, these can be powerful tools for developers and website owners.

{{ request }}

The request object is available on every page, and contains information regarding the HTTP request that may be useful for serving and rendering the page.

string

Can be any text, from the empty string ("") to the full HTML output of the template. When used alone in a conditional, all strings evaluates as true - even if they are empty or a value such as "0" or "false".

list

An enumerable list containing zero or more objects. Lists may contain many different object types, although most lists only contain a single object type. There are a large number of other complex object types that are also lists (ag: an articlelist is a complex object but is also a list of article objects).

Examples

Demonstrates how to access custom cookie.

Access custom cookie

Copy
{%- if cookies.customCookieName -%}
{%- assign cookieValue = cookies.customCookieName -%}
{%- elsif cookies['alternate-cookie-name'] -%}
{%- assign cookieValue = cookies['alternate-cookie-name'] -%}
{%- endif -%}
{%- if cookieValue != empty -%}
<p>Do something with {{ cookieValue }}</p>
{%- endif -%}

How to use the set_cookie method

Copy

Set a cookie that expires in 20 minutes

{%- var expiresDate = "now" | add_minutes: 20 -%}
{%- set_cookie loginsection "lastsection=accounts" expires:expiresDate path:"/protected" domain:".parentdomain.com" -%}
This example demonstrates the use of the expires, path, and domain parameters of the set_cookie method.

Set a cookie with a reference variable

{%- var sectioncookie = 'accountspage' -%}
{%- var sectioncount = cookies[sectioncookie] | to_int | plus: 1 -%}
{%- set_cookie &sectioncookie sectioncount -%}
This example demonstrates the use of a reference variable to set a cookie with a dynamic name.

Set a cookie with a custom statistics string

{%- if session.allowed and permissions.allow_public_statistics -%}
{%- capture statisticsString -%}
SessionStart: {{session.start_date | date: 'MMMM dd, yyyy, H:mm:ss'-}}
SessionRequests: {{session.num_requests-}}
LastRequest: {{request.date | date: 'MMMM dd, yyyy, H:mm:ss'-}}
<<Add other custom statistics here>>
{%- endcapture -%}
{%- set_cookie site_statistics statisticsString -%}
{%- endif -%}
Checks if the "allow_public_statistics" custom permission has been set, and if it has gathers information about the current session statistics for storage in a "site_statistics" cookie - presumably for display using javascript on the site.

Demonstrates how to list all cookies in the current request.

List all cookies in the current request

Copy
<h4>Cookies:</h4>
<ul>
{%- for cookie in request.cookies -%}
<li><strong>{{cookie}}</strong> = {{cookies[cookie]}}</li>
{%- endfor -%}
</ul>

How to use the unset_cookie method

Copy

Unset Cookie

{%- if cookie.advertising_id -%}
{%- unless permissions.allow_advertising -%}
{%- unset_cookie advertising_id advertising_alt_id -%}
{%- endunless -%}
{%- endif -%}
If the "advertising_id" cookie has been set and the "allow_advertising" permission has NOT been granted, use the unset_cookie method to clear both the "advertising_id" and "advertising_alt_id" cookies.

Unset multiple Cookies

{%- var unset_cookies = request.query_params.unsetcookies | split: ',' -%}
{%- for cookie in unset_cookies -%}
{%- var cookie_alt = cookie | append:'-alt' -%}
{%- unset_cookie &cookie &cookie_alt -%}
{%- endfor -%}
Checks if the "unsetcookies" query parameter was included in the request, and for each comma-delimited cookie name to unset, unset both the specified cookie and the "-alt" version of the cookie if it exists by using the unset_cookie method with two reference variables.