Liquid
Liquid Dot Notation

Exploring the Context Object

17min
want to use liquid dot notation to find dynamic data on your site? the context variable is most likely the place to look prerequisites in this article, we'll be using dot notation, so if you're not familiar with it, you may want to brush up here getting started with dot notation advanced dot notation arrays and key maps you might not need to fully understand this topic but a first read of it will be very useful in helping you to recognise the more difficult to handle types of data structure even if you're not ready to tackle them yet introduction once you've got a good understanding of liquid dot notation, the next step is branch out and discover what is possible here are some of the most useful places to look for data {{context}} the platformos object context contains a huge tree of information about your site often including variables you didn't know you wanted until you discovered them {{context exports}} the object exports sits on the root level of context and its role is to allow a global storage location for any custom variables not already included in context you'll mostly use it as a place to access siteglide specific data, for example, it is the home of data relating to siteglide categories and will contain webapp or module data when you use the collection parameter in the include tag exports will also contain any keys that you have entered within the integrations area in siteglide admin you can also add your own data to a namespace in exports see the platformos docs to see how this is especially useful if you want to include some data in a content section, code snippet or layout but access it in on a higher level e g the page template {{this}} this is a siteglide variable containing data specific to a layout you'll only be able to use if within a layout file the data will completely change depending on the type of content, so it's a good place to use dot notation to explore each time you try a new feature in this article, we'll explore the context object in more detail the this object is used differently in different features, so the best place to learn about it is by reading the documentation on the specific feature you're using it with context the context object's role is to provide your site with contextual information which might help it render html in a more flexible and personalised way context should be available in any liquid file, and this includes auto responder and workflow notifications however, bear in mind that context may work subtly differently in an email for example it's not possible to see information about the person reading an email current user would refer to the person submitting the form which triggers the email to be queued we'll go through the top level of context's keys and explain each namespace's role beyond that, you will have to explore for yourself! if a property is marked advanced only , it is because we haven't found a use case for it yet on a siteglide site, or they are not supported, or there is a newer version of this property which is easier to use we may mark a feature as advanced only because it is possible for it to conflict with a siteglide feature and it needs to be used with caution you may be able to discover your own use cases for these if so we'd be very interested to hear about them! to be clear, we can't offer support if these are used in your site, but we're noting them here in order to be comprehensive authenticity token (value) advanced only you won't need to use this directly it is a frequently changing token used as part of the security on form submissions session (object) platformos uses a single cookie to identify a visitor whether signed in or not the session object holds information in the database which relates to the visitor with that cookie this is used in siteglide ecommerce to allow us to identify someone who has added items to a cart, without requiring them to sign in learn more about how we use this essential cookie here adding session fields you can add fields to session and exports objects only the easiest way to add session fields is to use the {% session %} tag https //documentation platformos com/api reference/liquid/platformos tags#session compatibility to avoid conflicting with future siteglide features, we recommend that if you use this feature, you prefix any new fields with the initials or name of your agency e g let's say a user has decided to opt in to a particular non essential feature or cookie you could remember this {% session agency optional features = 'true' %} you can then use logic to only show these features to users who have opted in {% if context session agency optional features == 'true' %} {% endif %} removing session fields to continue our previous example, if the user chooses to change their preferences and opt out, you can forget the setting by setting it to an empty string {% session optional features = '' %} ending a session advanced only you can use custom graphql to completely forget a visitor and end their session use at your own risk as we cannot support secure zones with users who have had their session ended in this way https //documentation platformos com/api reference/graphql/mutations#user session destroy current user (object) unlike session , this only holds information relating to a user if they are currently logged in to a secure zone it is more sensitive than `session` and will include a name and email address if there is no current user, it will hold the value null headers (object) headers are information sent and received when a browser sends a request to a server for an html page this is a technical area which can be nevertheless useful on some every day use cases for example, in this article, we show you how to set up canonical urls we use the headers object to grab a useful version of the url https //developers siteglide com/preventing duplicate content with query parameters canonical url and robotstxt another helpful property is http referer this tells you the url of the page that the user was visited before the current one assuming this was not deliberately hidden this can be really useful for redirecting a user back to a particular page after carrying out an action params (object) this object helpfully takes the url of the current page and dissects it into chunks which you can use slugs refer to parts of a url separated by forward slashes / slug, slug2 and slug3 will be available to you only if they exist this can be helpful if you're implementing breadcrumbs query parameters will become available to you if they exist and will be available under the same key they use in the url e g a parameter in the url https //siteglide com?password=test will become available here {{context params password}} advanced only when using cli params can also be used in a form configuration to interrogate the form data being sent to the server, including virtual fields language (value) advanced only you can ignore this, as siteglide does not support this pos feature environment (value) advanced only this will currently only read the value of "production" as currently all siteglide sites are production ready but watch this space! is xhr (value) advanced only returns true if the page is being rendered by an xhr (sometimes referred to as ajax) request location (object) this is analogous to "location" in javascript and can provide you with useful url information {{context location url}} will get you the full absolute url for the current page {{context location href}} will get you the relative url for the current page without query parameters useful if you want to set new query parameters! {{context location pathname}} will get you the relative url for the current page including query parameters this object is often useful when you use it alongside params page (object) this contains metadata for the page layout (object) this actually gives you metadata relating to the chosen page template , not about layouts on the page modules (object) this contains objects for each module you have installed on your site it includes siteglide modules and any modules you've installed via platformos it only contains metadata if you want data relating to items in that module, you'll have to access it in a layout with {{this}} or through making a collection and then looking in {{context exports}} visitor (object) advanced only contains the ip address of the visitor constants advanced only useragent advanced only use device instead! device device should give you information about your visitor's device, operating system and browser however, use it with caution you'll still need to make websites responsive! you could use this to suggest to certain users that they will have a better experience if they update their browser to a modern one cookies this lists the cookies currently used by the site siteglide only uses the session cookie directly which you can learn more about here https //help siteglide com/en/article/the cookies we use on a site you build with siteglide yjurtu/ any payment gateways you are using as part of siteglide ecommerce e g stripe may also have a cookie listed here you may also see cookies relating to browser extensions ignore these as your users will have different extensions to your developers! version advanced only post params advanced only flash advanced only exports this is where all context data is kept which is custom either to siteglide or that you have added to your site