Variable tags


Variable tags allow you to define new Liquid variables.

Note: To ensure access to all Liquid objects, avoid naming your variables the same as predefined objects. Variables that match predefined object names can override those objects, limiting access to them.


assign

Creates a new variable.

You can define variables for different types of data, such as numbers, text, or more complex structures like objects and their properties.

syntax:

{% assign variable_name = value %}

variable_name

The name of the variable being created.

value

The content you wish to assign to the variable.

{%- assign product_title = product.title | upcase -%}
{{ product_title }}

capture

Creates a new variable with a string value.

You can generate intricate strings using Liquid logic and variables.

syntax:

{% capture variable %}
value
{% endcapture %}

variable

The name of the variable being created.

value

The content you wish to assign to the variable.

{%- assign up_title = product.title | upcase -%}
{%- assign down_title = product.title | downcase -%}
{%- assign show_up_title = true -%}

{%- capture title -%}
{% if show_up_title -%}
Upcase title: {{ up_title }}
{%- else -%}
Downcase title: {{ down_title }}
{%- endif -%}
{%- endcapture -%}

{{ title }}

decrement

Creates a new variable with an initial value of -1, which decreases by 1 each time it's used.

Variables declared using decrement are specific to the layout, template, or section where they are created. However, these variables are shared across snippets included in the same file.

Unlike variables created with assign and capture, those created with decrement are independent. However, variables using decrement and increment are shared.

{% decrement variable_name %}

variable_name

The identifier for the variable being reduced in value.


increment

Creates a new variable with a starting value of 0. Each time it's called, the variable increases by 1.

Variables declared using this method are specific to the layout, template, or section where they are created. However, they can be used across different snippets within the same file.

These variables behave differently from those created using other methods like assign and capture. However, they can interact with variables used in increment and decrement operations.

{% increment variable_name %}

variable_name

The identifier of the variable that is being increased.