Context Variable

Last updated on June 26, 2024.

The Context variable is a globally accessible object in every Liquid project file, such as views, Authorisation Policies, and notifications.

Displaying the context object

You can display the context object on the page to inspect its structure and content, as shown in the following example:

Context object components

This section discusses the following context object components:

  • context.authenticity_token
  • context.constants
  • context.cookies
  • context.current_user
  • context.device
  • context.environment
  • context.flash
  • context.headers
  • context.is_xhr
  • context.language
  • context.location
  • context.modules
  • context.page
  • context.params
  • context.session
  • context.useragent
  • context.visitor

context.authenticity_token


is populated by the server and automatically included in every form generated by the tag. It is used to mitigate Cross-Site Request Forgery attacks. It has to be added to the payload in all server requests other than GET.

context.constants

gives you access to sensitive data like API credentials and tokens. To protect yourself from leaking them, call  explicitly, as they are hidden from . You can set constants using GraphQL mutation .

context.cookies

return an object with all cookies of the site.

context.current_user

returns basic data of the currently logged-in user. Available attributes are:

This context variable returns instead of an object if the user is not logged in.

context.device

returns a hash with useful information based on UserAgent. Most notably, returns one of the following:

  • desktop
  • smartphone
  • tablet
  • console
  • portable media player
  • tv
  • car browser
  • camera

context.environment

returns a string: or . A common use case is hiding some production functionalities without blocking the release.

context.flash

returns object with possible keys: alert, notice. The value for each key is a message string. are set on form submissions.

context.headers

contains allowed HTTP headers with the data from the current HTTP request. Available headers are:

  • SERVER_NAME
  • REQUEST_METHOD
  • PATH_INFO
  • REQUEST_URI
  • HTTP_AUTHORIZATION
  • HTTP_HOST
  • HTTP_USER_AGENT
  • HTTP_REFERER
  • SERVER_PORT
  • QUERY_STRING
  • X-FORWARDED-FOR
  • HTTP_STRIPE_SIGNATURE
  • HTTP_AUTHENTICATION_TOKEN

context.is_xhr

returns for non-XHR and Boolean for XHR requests. A request is identified as XHR by checking if the value of the header matches .

context.language

returns a String, the language ISO code used in the current request.
Go to the translations section for more information about the built-in translation mechanism.

context.location

contains URL data of the current request. Available attributes are:

  • href
  • host
  • pathname
  • search

Example 1: Extracting params from path name with 'extract_url_params'

URL:

Example 2: Conditional list class

context.modules

Information about the version and subscription status of installed modules.

Example: List all properties for installed modules

context.page

contains information about the page on which it is included.

Available attributes are:

context.params

contain user data provided in forms and query params.

The most common use cases are:

  • Include form data in notifications
  • Form data manipulation through default_payload
  • Data validation in Authorisation Policies

Example 1: site path params mapping

The value of when visiting the page with the path would be:

Another way of extracting page params is using .

context.session

allows for quick access to session storage. Use mutations to store data in a session like in this example.

The browser generates a session ID, stores it as a cookie, and then sends it with every request. On the server side, the system checks if an entry matches this session_id and gets the associated JSON. This JSON can contain anything, including user_id if the user is logged in, but if the user is not logged in, you can still use the session_id. As mentioned, it can contain other data - for example, product_ids to power the shopping cart functionality for a user that is not logged in.

After some time, data associated with the session_id is removed when the server invalidates the session.
A user can also invalidate the session by manually removing the session_id, and then the browser will generate a new one.

context.useragent

returns an object with user agent data.

Example 1: Displaying useragent

context.visitor

returns an object describing the browser user. For now, there is only one available attribute .

Exposing a local variable within the context object

It is possible to promote a local variable, so it is available in the namespace. Use the tag to do that as in the following example:

Have a suggestion for this page?

Didn't quite find what you are looking for or have feedback on how we can make the content better then we would love to hear from you. Please provide us feedback and we will get back to you shortly.