{{ liquidContext }} object

This Liquid object is globally accessible in every liquid template or layout and outputs a combination of the {{this}} and {{request}} Liquid objects as well as any Liquid variables available to the layout it is placed in.

{{ liquidContext }}

Below is a live example of the {{liquidContext}} output for this very documentation article, where a Liquid variable called sampleVar has been assigned in the content.
You'll notice both the request and this data collections are output along with the sampleVar variable that was included in this articles content.

The main purpose of this Liquid object is to assist with easier access to Liquid data while developing your site. See further below for examples of accessing this data during development.

{
  "request": {
    "request_url": {
      "href": "https://docs.treepl.co/documentation_group/liquid/liquidcontext-object",
      "origin": "https://docs.treepl.co",
      "protocol": "https",
      "hostname": "docs.treepl.co",
      "path": "/documentation_group/liquid/liquidcontext-object",
      "params": {}
    },
    "cookies": {
      "asp.net_sessionid": "bmx1a5q5dasifyp5xewi0yh5"
    },
    "request_data": {
      "ip": "18.232.51.69",
      "is_mobile": 0,
      "user_agent": "CCBot/2.0 (https://commoncrawl.org/faq/)",
      "referrer": null
    },
    "timezone": {
      "label": "(UTC-06:00) Central Time (US & Canada)",
      "offset": -5.0
    },
    "is_logged": false,
    "currentmember": null,
    "device_type": "Desktop"
  },
  "this": {
    "Id": 2444,
    "Url": "/documentation_group/liquid/liquidcontext-object",
    "ParentId": 1881,
    "LastUpdatedDate": "2019-09-10T00:54:47",
    "Active": true,
    "AdditionalRelatedArticle_Name": null,
    "AdditionalRelatedArticle_Url": null,
    "AdditionalRelatedArticle": "",
    "AdditionalRelatedArticle2_Name": null,
    "AdditionalRelatedArticle2_Url": null,
    "AdditionalRelatedArticle2": "",
    "AMPContent": "",
    "Author_Name": null,
    "Author_Url": null,
    "Author": "",
    "Authors": "2418",
    "CanonicalLink": "",
    "CodeEditor": true,
    "CreatedByMemberId": 0,
    "Description": "<p>This Liquid object is globally accessible in every liquid template or layout and outputs a combination of the <code>{% raw %}{{this}}{% endraw %}</code> and <code>{% raw %}{{request}}{% endraw %}</code> Liquid objects as well as any Liquid variables available to the layout it is placed in.</p>\n{% capture data %}{% raw %}{{ liquidContext }}{% endraw %}{% endcapture %}\n{% component type: \"snippet\", alias: \"code_block_processor\", data: \"{{data}}\", lang: \"liquid\" %}\n{% assign sampleVar = 'My Sample Value' %}\n<p>Below is a live example of the <code>{% raw %}{{liquidContext}}{% endraw %}</code> output for this very documentation article, where a Liquid variable called <b>sampleVar</b> has been assigned in the content.<br>\nYou'll notice both the <code>request</code> and <code>this</code> data collections are output along with the <code>sampleVar</code> variable that was included in this articles content.</p>\n<p>The main purpose of this Liquid object is to assist with easier access to Liquid data while developing your site. See further below for examples of accessing this data during development.</p>\n\n{% component type: \"snippet\", alias: \"code_block_processor\", data: \"{{liquidContext}}\", lang: \"json\" %}\n\n{% component type: \"snippet\", alias: \"section_accessing_data\" %}\n<p>As an example of rendering a desired value to the page you could use the following Liquid:</p>\n{% capture data %}\n{% raw %}\n{{ liquidContext.this.url }}\n{% endraw %}\n{% endcapture %}\n{% component type: \"snippet\", alias: \"code_block_processor\", data: \"{{data}}\", lang: \"liquid\" %}\n\n<p>This would output the value <code>{{liquidContext.this.url}}</code> to the page based on the above data.</p>\n\n{% component type: \"snippet\", alias: \"section_viewing_data_while_developing\", object: \"liquidContext\", filter: \" | strip_html\" %}",
    "DisableForSiteSearch": false,
    "EnableAMP": false,
    "Enabled": true,
    "ExpiryDate": "2099-11-29T00:00:00",
    "ExternalId": 0,
    "ExternalResources": "",
    "IgnoreUpdates": false,
    "ItemCategories": null,
    "ItemCategoryIdList": null,
    "ItemTags": [
      "Liquid Usage"
    ],
    "MetaDescription": "",
    "Name": "{{&nbsp;liquidContext&nbsp;}} object",
    "OpenGraphProperties": {
      "title": "",
      "type": "",
      "url": "",
      "locale": "",
      "image": ""
    },
    "ReleaseDate": "2018-08-04T00:00:00",
    "SeoPriority": "",
    "SEOTitle": "",
    "MetaTitle": "",
    "ShowPageForSearchEngine": true,
    "SiteSearchKeywords": "",
    "SKUCode": "",
    "SocialMetaTags": "",
    "UpdateNotes": "All new article",
    "UrlSlug": "liquidcontext-object",
    "Weighting": 977,
    "parentname": "Liquid Objects & Usage",
    "parenturl": "/documentation_group/liquid",
    "templatename": "Docs Template",
    "ishome": false
  },
  "data": "{{ liquidContext }}",
  "params": null,
  "sampleVar": "My Sample Value"
}

Accessing the Data

As an example of rendering a desired value to the page you could use the following Liquid:

{{ liquidContext.this.url }}

This would output the value /documentation_group/liquid/liquidcontext-object to the page based on the above data.

Viewing Data while Developing

It is often desirable to see all the data that is available to you on a page while developing your applications and there are two common techniques for doing this.

1. Rendering JSON to the Page

You can quickly and easily render the full JSON output directly to the page you are working on in order to view all the data and its structure in an easy to read format.
Simply wrap your desired Liquid object in <pre> tags like so; <pre>{{liquidContext}}</pre>

A JSON representation of the available data will be rendered to the page similar to the output examples above.

You can actually use this method to log any Liquid data to the console, such as a custom collection, for example: <pre>{{myCustomCollection}}</pre>

This of course is a temporary technique as you would not want to leave the JSON data visible on the page.

2. Rendering JSON to the Console

Perhaps a cleaner and more persistent way of viewing this data while in development, is to log the JSON output to your browser's console (in the browser's Developer Tools).

To do this, add some javascript code to your template file (just before the closing </body> tag) like this:

<script>
    console.log({{liquidContext | strip_html}});
</script>

What this will do is output the JSON data into a structured data tree in your browser's Developer Tools console.

You can actually use this method to log any Liquid data to the console, such as a custom collection, for example:

<script>
    console.log({{myCustomCollection | strip_html}});
</script>

You might notice the Liquid filter | strip_html added here. This is optional, however, in some cases the JSON data will contain HTML code and this can break the Javascript, causing a scripting error.
So, keep in mind that, when using this filter, any fields containing HTML code in your JSON will not actually show the HTML in the console, however, the data is still there and accessible via Liquid when rendering to the page.

Remember to remove this code for production as it could pose a data security risk and it is best practice not to leave console logs in your scripts.



Related Articles

External Resources

There are currently no external resources available.

Please let us know if you have any other contributions or know of any helpful resources you'd like to see added here.


Questions?

We are always happy to help with any questions you may have.
Send us a message at support@treepl.co and we will consult you as soon as possible.