Homepage

Liquid - Tags: Variables

Last edit: Sep 16, 2024

platformOS customization note

Variables (no matter how defined) in platformOS are local. This means that a variable defined inside a partial is not available in a page that includes it. To export variables from a partial read about the export tag.

assign

Creates a new named variable.

Input


{% assign favorite_food = 'apples' %}

My favorite food is {{ favorite_food }}.

Output

My favorite food is apples.

assigned variables can be strings or booleans (true or false). Remember not to use quotation marks around the value if it is true or false:

Input


{% assign first_time_visitor = true %}
{% if first_time_visitor == true %}
Welcome to the site!
{% endif %}

Output

Welcome to the site!

Note

platformOS has also available hash_assign tag, which is based on assign tag, however it allows you to work with your own objects

capture

Captures the string inside of the opening and closing tags and assigns it to a variable.
Variables that you create using capture are stored as strings, so it works exactly like the assign tag, but allows you to assign complex, multiline data using other variables and liquid tags.

parse_json

It is platformOS specific tag, which allows you to create your own objects. It works similarly to capture.

The content within this tag must result in valid JSON. Otherwise, an exception is thrown, which is logged automatically using the same mechanism as the log tag.
When working with parse_json and user input, it's advisable to use the json filter to prevent special characters like " from disrupting the parsing process.

Input


{% assign favorite_food = 'pizza' %}
{% assign age = 35 %}

{% parse_json about_me %}
  {
    "favorite_food": {{ favorite_food | json }},
    "age": {{ age }}
  }
{% endparse_json %}

{{ about_me }}

Output

  {
    "favorite_food": "pizza",
    "age": 35
  }

Tip

The most common error when working with a parse_json tag is missing a comma (or having a trailing comma at the end of the json).

increment

Creates a new number variable, and increases its value by 1 every time increment is called on the variable. The counter's initial value is 0.

Here, an increment counter is used to create a unique numbered class for each list item:

Input


<ul>
  <li class="item-{% increment counter %}">apples</li>
  <li class="item-{% increment counter %}">oranges</li>
  <li class="item-{% increment counter %}">peaches</li>
  <li class="item-{% increment counter %}">plums</li>
</ul>

Output

<ul>
  <li class="item-0">apples</li>
  <li class="item-1">oranges</li>
  <li class="item-2">peaches</li>
  <li class="item-3">plums</li>
</ul>

Variables created using increment are separate from variables created using assign or capture.

In the example below, a variable named my_number is created using assign.
The increment tag is then used several times on a variable with the same name. Note that the increment tag does not affect the value of my_number that was created through assign.

Input


{% assign my_number = 10 %}

{% increment my_number %}
{% increment my_number %}
{% increment my_number %}

{{ my_number }}

Output

0
1
2

10

decrement

Creates a new number variable, and decreases its value by 1 every time decrement is called on the variable. The counter's initial value is -1.

Input


{% decrement variable %}
{% decrement variable %}
{% decrement variable %}

Output

-1
-2
-3

Like increment, variables declared using decrement are independent from variables created using assign or capture.

Note

This topic is a compilation of knowledge found at: Shopify Themes, Liquid Documentation, Liquid Gem Documentation, and Liquid for Designers.

Questions?

We are always happy to help with any questions you may have.

contact us