Liquid Variables
A liquid variable is a stored value, code block, or object assigned to a custom name that can then be referenced in liquid code.
Variables can also be reassigned throughout the executed code and manipulated with the use of filters or other Liquid processing.
To create variables within the Liquid scope, use the following Liquid tags:
assign
Assigns a value (such as a string, number, boolean or an existing Liquid object) to a named reference. Then, use the variable with your named reference wrapped in double curly brackets (unless being used within another Liquid tag).
When storing a string, use single or double quotation marks around the value.
{% assign myName = "Alex" %}
{{ myName }}
Alex
When storing a boolean or number, omit the quotation marks around the value.
{% assign myBoolean = true %}
{% if myBoolean != false %}
My boolean is TRUE.
{% else %}
My Boolean if FALSE.
{% endif %}
My boolean is TRUE.
Working with numbers and reassigning the value.
{% assign myCount = 3 %}
{% for i in (1..4) %}
{% assign myCount = myCount | plus: 1 %}
{{ myCount }}<br>
{% endfor %}
4
5
6
7
You can store an object in a variable (such as items from a Liquid module collection).
{% component type: "module", source: "Blog Post", layout: "", collectionVariable: "blogPostCollection" %}
{% assign myItems = blogPostCollection.items %}
{% for post in myItems %}
<strong>{{ post.['Name'] }}</strong>
<div>{{ post.['MetaDescription'] }}</div>
<hr>
{% endfor %}
My First Post Title
The SEO Meta description of my first post.
My Second Post Title
The SEO Meta description of my second post.
My Third Post Title
The SEO Meta description of my third post.
capture
Create a named variable from multiple lines of content or code by wrapping it in Liquid capture
tags.
The original captured content will not be rendered and will only be available from the variable as a string value.
{% capture myVarName %}
<strong>My Heading</strong>
My paragraph of text.
{% endcapture %}
<p><em>My Output:</em></p>
{{myVarName}}
My Output:
My Heading
My paragraph of text.
You can use Liquid inside a capture
to construct more complex strings.
{% assign myName = "Alex" %}
{% assign myInterests = "Singing,Dancing,Stargazing" | split: ',' %}
{% capture myProfile %}
My name is {{ myName }} and I like:<br>
{% for topic in myInterests %}
{{ topic }}<br>
{% endfor %}
{% endcapture %}
<p><em>My Profile:</em></p>
{{myProfile}}
My Profile:
My name is Alex and I like:
Singing
Dancing
Stargazing
increment
Creates a number variable, starting at 0
and increments that value with each subsequent tag used in the document flow (including from layouts, for example; from a Content Template down into a Page).
Unlike other variables, this tag will simply output its value ‘in situ’ and its name is used to distinguish the count from other possible increment
tags meant for a separate sequence.
{% increment myCount %}
{% increment myCount %}
{% increment myCount %}
0
1
2
increment
variables are independent from other variables, even with the same name, and do not affect the values of other variables (except for other matching decrement
tags), nor are they themselves affected by other variables.
{% increment myCount %}
{% assign myCount = 100 %}
{% increment myCount %}
{% increment myCount %}
{{ myCount }}
0
1
2
100
decrement
Creates a number variable, starting at 0
and decreases that value with each subsequent tag used in the document flow (including from layouts, for example; from a Content Template down into a Page).
Unlike other variables, this tag will simply output its value ‘in situ’ and its name is used to distinguish the count from other possible decrement
tags meant for a separate sequence.
{% decrement myCount %}
{% decrement myCount %}
{% decrement myCount %}
0
-1
-2
decrement
variables are independent from other variables, even with the same name, and do not affect the values of other variables (except for other matching increment
tags), nor are they themselves affected by other variables.
Below we'll see the influence between matching increment
and decrement
tags when used together.
{% increment myCount %}
{% assign myCount = 100 %}
{% increment myCount %}
{% decrement myCount %}
{% increment myCount %}
{{ myCount }}
{% decrement myCount %}
{% decrement myCount %}
0
1
0 <!-- the previous increment is reduced by 1 -->
1 <!-- this increment carrys on from the previous value -->
100
0 <!-- the previous value is again reduced by 1 -->
-1