For loop
Video outline
Definition
A for loop repeatedly executes a block of code
We can use a for loop to create a long list with less code and prevent manual input. The loop is used in liquid to create a specific number of iterations. The loop ends once all data has been run through.
Syntax
The for loop should always be opened and closed.
{% for item in collection %}
{% endfor %}
The item is the keyword which we will name the for loop, name it without spaces or punctuation.
The collection is where we provide a set of data or collection for the database.
How to use the for loop
To use the forloop we need a set of data and define it.
{% assign test_array = "test 1| test 2| test 3| test 4" | split:"|" %}
{% for item in test_array %}
{{ item }}
{% endfor %}
A for loop can also be used to loop through information from the database
{% for employee in custom.my_employees %}
{{ employee.name }}
{% endfor %}
Updated about 3 years ago