Logic tags
Logic tags are statements you can run wrapped in these tags {% %}.
The liquid logic statements available to you are:
assign
Assign statements create new variables and you can assign value to be stored in the variables
Input
{% assign variable_1 = true %}
{{variable_1}}
Output
true
capture
Captures whatever is inside of it, and assigns the result to a variable. The result is always a string.
{% capture variable_1 %}<p>hello world.</p>{% endcapture %}
case
Case statements are useful when there are multiple cases you want to check in a variable.
Input
{% case day %}
{% when 'monday' %}
Today is monday
{% when 'tuesday' %}
Today is Tuesday
{% else %}
Today is not monday or Tuesday
{% endcase %}
Output
This is a cake
comment
Comment statements hide the text placed between them. It is used as a note to the author of the liquid
Input
Now you {% comment %}now you don't{% endcomment %} see me
Output
Now you see me
for
To loop through a list (an array) of things one element at a time.
Input
{% for item in items %}
{{ item.name }}
{% endfor %}
if
If statements determine if something is true or not and run the code enclosed by if and endif if the condition is met.
Input
{% if form.name == 'invoice' %}
form name is invoice
{% endif %}
Output
form name is invoice
if / else / elsif
Else sand elsif tatements add another condition to an "if" statement.
Input
{% assign name = 'invoice' %}
{% if name == 'invoice' %}
form name is invoice
{% elsif name == 'quote' %}
form name is invoice
{% else %}
form name is else
{% endif %}
Output
form name is invoice
increment
Outputs the value of a variable (outputs zero if it wasn't defined before), then increments it.
{% increment variable %} -- outputs 0, then increments
{{ variable }} -- outputs 1
{% increment variable %} -- outputs 1, then increments
{{ variable }} -- outputs 2
Information
To output a number in a for loop that increments with each iteration, use forloop.index. If you don't want to output a value, yet increment it, use {{ assign value = value | plus: 1 }}.
decrement
Same as increment, but decrements the variable (-1 instead of +1).
tablerow
Tablerow property generates table rows. Adding in a "cols:x" statement will generate columns, where 'x' is the number of columns.
Input
{% tablerow product in collection.products cols:1 %}
{{ product.title }}
{% endtablerow %}
Output
<table>
<tr>
<td>hat</td>
<td>shirt</td>
</tr>
<tr>
<td>pants</td>
</tr>
</table>
unless
Unless statements set a condition that "unless" this statement is true than something else will happen.
Input
{% unless customer.first_name == 'bob' %}
name is bob
{% endunless %}
Output
name is bob
Comments and Suggestions
0 comments
Please sign in to leave a comment.
Related articles