{{ this }} object

This Liquid object is globally accessible in every liquid template or layout and outputs specific data relating to the item being render.

{{ this }}

For example, if {{this}} is placed in a Page content area it will render the liquid data of that page. Likewise if placed in a Module content field, it will render the liquid data available for the specific module item being viewed.

Below is a live example of the {{this}} output for this very documentation article.

{
  "Id": 2324,
  "Url": "/documentation_group/liquid/this-object",
  "ParentId": 1881,
  "LastUpdatedDate": "2020-03-27T03:43:34",
  "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 specific data relating to the item being render.</p>\n{% capture data %}{% raw %}{{ this }}{% endraw %}{% endcapture %}\n{% component type: \"snippet\", alias: \"code_block_processor\", data: \"{{data}}\", lang: \"liquid\" %}\n<p>For example, if <code>{% raw %}{{this}}{% endraw %}</code> is placed in a Page content area it will render the liquid data of that page. Likewise if placed in a Module content field, it will render the liquid data available for the specific module item being viewed.</p>\n\n<p>Below is a live example of the <code>{% raw %}{{this}}{% endraw %}</code> output for this very documentation article.</p>\n\n{% component type: \"snippet\", alias: \"code_block_processor\", data: \"{{this}}\", 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{{ 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>{{this.url}}</code> to the page based on the above data.</p>\n\n{% component type: \"snippet\", alias: \"section_viewing_data_while_developing\", object: \"this\", filter: \" | strip_html\" %}\n\n{% comment %}\n\n\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{{ request.request_data.is_mobile }}\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>0</code> to the page based on the above example data.</p>\n<p>By the way, the <code>is_mobile</code> parameter is a boolean indicating whether the device being used is a mobile device or not. Where <code>0</code> indicated it is not a mobile device and <code>1</code> indicated it is a mobile device.</p>\n<p>Extending on this example, we can use this value in a conditional Liquid statement to display different content on a page for mobile devices like so:</p>\n{% capture data %}\n{% raw %}\n{% if request.request_data.is_mobile == 1 %}\n    <p>You're viewing mobile enabled content!</p>\n{% else %}\n    <p>You're viewing desktop content!</p>\n{% endif %}\n{% endraw %}\n{% endcapture %}\n{% component type: \"snippet\", alias: \"code_block_processor\", data: \"{{data}}\", lang: \"liquid\" %}\n\n<p class=\"notice-note\">When using the output values in Liquid logic they are case sensitive. So, <code>{% raw %}{% if request.device_type == \"desktop\" %}...{% endraw %}</code> will not work, whereas <code>{% raw %}{% if request.device_type == \"Desktop\" %}...{% endraw %}</code> will work.</p>\n\n\n{% endcomment %}\n",
  "DisableForSiteSearch": false,
  "EnableAMP": false,
  "Enabled": true,
  "ExpiryDate": "2099-11-29T00:00:00",
  "ExternalId": 0,
  "ExternalResources": "",
  "IgnoreUpdates": true,
  "ItemCategories": [
    "Sample Category"
  ],
  "ItemCategoryIdList": [
    "3"
  ],
  "ItemTags": [
    "Liquid Usage"
  ],
  "MetaDescription": "",
  "Name": "{{&nbsp;this&nbsp;}} object",
  "OpenGraphProperties": {
    "title": "",
    "type": "",
    "url": "",
    "locale": "",
    "image": ""
  },
  "ReleaseDate": "2018-08-04T00:00:00",
  "SeoPriority": "",
  "SEOTitle": "",
  "MetaTitle": "",
  "ShowPageForSearchEngine": true,
  "SiteSearchKeywords": "",
  "SKUCode": "",
  "SocialMetaTags": "",
  "UpdateNotes": "",
  "UrlSlug": "this-object",
  "Weighting": 985,
  "parentname": "Liquid Objects & Usage",
  "parenturl": "/documentation_group/liquid",
  "templatename": "Docs Template",
  "ishome": false
}

Accessing the Data

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

{{ this.url }}

This would output the value /documentation_group/liquid/this-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>{{this}}</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({{this | 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.