How to use for break limit in Shopify
Drive 20-40% of your revenue with Avada
Since 2006, Liquid has been used in Shopify as a language template. In Liquid, you use tags consisting of comment, control flow, iteration, raw and variable to show complex content. In this tutorial, we will introduce you about some types of liquid iteration including for, break and limit.
Table of content
- Brief guide to Liquid
- General information about Liquid iteration
- How to use for break limit iteration
Brief guide to Liquid
There are three types in Liquid codes including objects, tags and filters. Liquid use objects to show the location of the content on a page. Double curly braces denote objects and variable names. Filters change a Liquid object’s output. Tags create logic and control flow for a template. Tags begin with two curly braces and percent signs. Tags can be divided into three types: control flow, iteration and variable assignment.
Iteration consists of several typical types that can help you display information on your front page. Today tutorial focuses on for, break and limit iteration.
General information about Liquid iteration
Iteration tags run blocks of codes repeatedly. For tag repeatedly executes a block of code.
How to use for break limit iteration
First of all, for
tag repeatedly execute a block of code for a full list of attributes available within a for
loop.
Input
{% for product in collection.products %}
{{ product.title }}
{% endfor %}
Output
hat shirt pants
Second, break
iteration causes the loop to stop iterating once it encounters this tag.
Input
{% for i in (1..5) %}
{% if i == 4 %}
{% break %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
Output
1 2 3
Third, limit
iteration limits the loop to the specified number of iterations. For instance, you have a chain of number and you want to show only two numbers of that chain on your website, then you should do:
Input
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array limit:2 %}
{{ item }}
{% endfor %}
Output
1 2
Conclusion
In conclusion, Liquid provides three iteration tags to you to execute blocks of codes. You can use tags of for, break and limit in order to upload the chain of characters. We hope that the tutorial is helpful, and let us know your difficulties, we will address them.