Set_cookie

Set_cookie

Examples

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.