{{ request }} object

This Liquid object is globally accessible in every liquid template or layout and outputs various data relating to your site.

{{ request }}

An live render of the data output is below.

{
  "request_url": {
    "href": "https://docs.treepl.co/documentation_group/liquid/request-object",
    "origin": "https://docs.treepl.co",
    "protocol": "https",
    "hostname": "docs.treepl.co",
    "path": "/documentation_group/liquid/request-object",
    "params": {}
  },
  "cookies": {
    "asp.net_sessionid": "35uf5fryiisitgaeixm5w0mn"
  },
  "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"
}

currentmember

When a user is logged in to a secure zone, the currentmember property above will return an object with the logged in member details. An example is below:

{
    
    ...
    
    "is_logged": true,
    "currentmember": {
        "id": 162,
        "email": "asmith@example.com",
        "firstname": "Alex",
        "lastname": "Smith",
        "address": null,
        "city": null,
        "state": null,
        "zipcode": null,
        "country": "Australia",
        "site": null,
        "phone": null,
        "createddatetime": "2018-11-21T15:17:22.717",
        "updateddatetime": "2018-11-21T15:17:22.763",
        "securezones": [
            {
            "id": 2,
            "name": "Members Secure Zone",
            "landingpageid": 2541,
            "createddatetime": "2018-11-21T15:17:23.037",
            "updateddatetime": "2018-11-21T15:17:23.037"
            }
        ]
    }
}

device_type

The possible values provided by the device_type property are:

  • Desktop: when a desktop browser is rendering the page.
  • Tablet: when a tablet browser is rendering the page.
  • Mobile: when a mobile/smart phone browser is rendering the page.

A typical use case for this property is to display different content depending on the users device.

The following Liquid example will render different results on the three different device types:

{% case request.device_type %}
{% when 'Mobile' %}
    <p>I'm mobile</p>
{% when 'Tablet' %}
    <p>I'm an iPad or similar tablet.</p>
{% else %}
    <p>I must be a desktop.</p>
{% endcase %}

When using the output values in Liquid logic they are case sensitive. So, {% if request.device_type == "desktop" %}... will not work, whereas {% if request.device_type == "Desktop" %}... will work.

Accessing the Data

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

{{ request.request_data.is_mobile }}

This would output the value 0 to the page based on the above example data.

By the way, the is_mobile property is a boolean indicating whether the device being used is a mobile device or not. Where 0 indicated it is not a mobile device and 1 indicated it is a mobile device.

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:

{% if request.request_data.is_mobile == 1 %}
    <p>You're viewing mobile enabled content!</p>
{% else %}
    <p>You're viewing desktop content!</p>
{% endif %}

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>{{request}}</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({{request}});
</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.