Master Shopify Liquid in just 5 minutes.

There are three things to remember. 1) Objects 2) Tags – logic statement 3) Filters. Shopify Liquid is very easy if you have knowledge of other programming language. However Liquid is not a programming language, it is template language. Template language used to render information.

Prerequisite: You should have knowledge of other programming language. Then it is very easy to understand below topics.

Master Shopify Liquid in just 5 minutes.

Things to remember in Shopify Liquid

1) Objects

{{ Objects }} <= Written inside double curly braces.

2) Tags – logic statement

{% logic %} <= Written inside curly brace and percentage sign.

3) Filters.

After | (pipe) sign filter is used.

“assign” Keyword

It is used to assign value to variable. For example

{% assign myVariable =  "myVariable Value" %}

“if” Statement

{% if condition %}
expression
{% endif %}

elsif Statement

{% if condition %}
expression
{% elsif condition %}
expression
{% endif %}

else Statement

{% if condition %}
expression
{% else %}
expression
{% endif %}

unless Statement

{% unless condition %}
  expression
{% endunless %}

Similar to the if tag, you can use elsif to add more conditions to an unless tag.

case Statement

{% case variable %}
  {% when first_value %}
    first_expression
  {% when second_value %}
    second_expression
  {% else %}
    third_expression
{% endcase %}

for Tag

You can do a maximum of 50 iterations with a for loop. 

{% for variable in array %}
  expression
{% endfor %}

You can limit the number of iterations using the limit parameter.

{% for variable in array limit: number %}
  expression
{% endfor %}

You can iterate in reverse order using the reversed parameter.

{% for variable in array reversed %}
  expression
{% endfor %}

else in for loop : Allows you to specify a default expression to execute when a for loop has zero length.

{% for variable in array %}
  first_expression
{% else %}
  second_expression
{% endfor %}

break

Stops a for loop from iterating.

{% break %}

continue

Causes a for loop to skip to the next iteration.

comment

Any text inside comment tags won’t be output.

{% comment %}
  content
{% endcomment %}

Inline comments

{% # content %}

echo

Outputs an expression.

{% liquid
  echo expression
%}

liquid

Allows you to have a block of Liquid without delimeters on each tag.

{% liquid
  expression
%}

raw

Outputs any Liquid code as text instead of rendering it.

{% raw %}
  expression
{% endraw %}

decrement & increment

increment: Creates a new variable, with a default value of 0, that’s increased by 1 with each subsequent call.

decrement: Creates a new variable, with a default value of -1, that’s decreased by 1 with each subsequent call.

Array filters

FilterWork
array | compactRemoves any nil items from an array.
array | concat: arrayConcatenates (combines) two arrays.
array | find: string, string (property name and the associated value.)Returns the first item in an array with a specific property value.
array | find_index: string, stringReturns the index of the first item in an array with a specific property value.
array | firstReturns the first item in an array.
array | has: string, string
returns boolean
Tests if any item in an array has a specific property value.
array | join
returns string
Combines all of the items in an array into a single string, separated by a space.
array | join: stringYou can specify a custom separator for the joined items.
array | lastReturns the last item in an array.
array | map: stringCreates an array of values from a specific property of the items in an array.
array | reject: string, stringFilters an array to exclude items with a specific property value.
array | reverseReverses the order of the items in an array.
variable | size Returns the size of a string or array.
array | sortSorts the items in an array in case-sensitive alphabetical, or numerical, order.
array | sort: stringSort by an array item property
array | sort_naturalSorts the items in an array in case-insensitive alphabetical order.
array | sort_natural: stringYou can specify an array item property to sort the array items by.
array | sumReturns the sum of all elements in an array.
array | sum: stringSum object property values
array | uniqRemoves any duplicate items in an array.
array | where: string, stringFilters an array to include only items with a specific property value.
Filter for boolean properties with a true value. This requires you to provide only the property name.

This is a quick 5 minute master class for Shopify Liquid. If you are curious and want to know more please visit Shopify Liquid Official documentation link: https://shopify.dev/docs/api/liquid

Check our Android Tutorial in simple and easy language.