Getting cookie as a HubL variable

628 Views Asked by At

In HubSpot I'm trying to check if a cookie exists, if it does exist, I'm looking to perform an action through an if statement. However, I cannot seem to get the cookie value using request.cookies as outlined in their docs.

Doing the following:

{% set cookies = request.cookies %}
{{ cookies }}

Returns all the cookies. I'm looking to obtain a single cookie value in a variable.

The cookie I'm trying to obtain in a variable is named __hssluid. Since request.cookies is of type dict, I've tried the following:

{% set cookie_name = "__hssluid" %}
{% set result = request.cookies.cookie_name %}
{{ result }}

But it doesn't show anything. How can I get a single cookies value using HuBL?

Have also tried the following:

{% set cookie_name = "__hssluid" %}
{{ request.cookies[cookie_name] }}
{{ request.cookies.cookie_name }}
{{ request.cookies.__hssluid }}

All the above prints nothing for me.

2

There are 2 best solutions below

0
On

To get a value of a cookie with HUBL you just need to do:

{{ request.cookies.cookie_name }}

without setting the cookie_name as a variable

0
On

I used this because I ran into the same issue.

{% if request.cookies is containing 'cookie_name' %}
    Cookie exists
{% endif %}