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.

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.

Examples

{% 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'}} <> {%- endcapture %} {% set_cookie site_statistics statisticsString %} {% endif %}

Set Cookie that expires in 20 minutes

Copy
{% var expiresDate = "now" | add_minutes: 20 %} {% set_cookie loginsection "lastsection=accounts" expires:expiresDate path:"/protected" domain:".parentdomain.com" %}

Logout clear session and cookies

Copy
{% logout clear_session clear_cookies %}

Logout clear cookies

Copy
{% logout clear_cookies %}

Unset multiple Cookies

Copy
{% 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 %}

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 %}

Do something with {{ cookieValue }}

{% endif %}

Set cookie with reference variable

Copy
{% var sectioncookie = 'accountspage' %} {% var sectioncount = cookies[sectioncookie] | to_int | plus: 1 %} {% set_cookie §ioncookie sectioncount %}

List all cookies in the current request

Copy

Cookies:

    {% for cookie in request.cookies %}
  • {{cookie}} = {{cookies[cookie]}}
  • {% endfor %}

Unset Cookie

Copy
{% if cookie.advertising_id %} {% unless permissions.allow_advertising %} {% unset_cookie advertising_id advertising_alt_id %} {% endunless %} {% endif %}