mowen10
mowen10

Reputation: 393

What does {% if forloop.index <=1 %} in shopify code mean

I am just looking through a Shopify site that someone else built but unfortunately I can't get hold of him anymore.

So I have been asked to make some changes to prices and add an extra table of prices so came across a pages.prices.liquid template page that has the code that I need to edit but i am just trying to make sense of it all first before I dive in.

Is anybody able to tell me what the following means:

{% if forloop.index <=1 %}
...
{% elsif forloop.index <7 %}

would appreciate it if somebody could please advise.

Upvotes: 0

Views: 5559

Answers (2)

Jacques Cornat
Jacques Cornat

Reputation: 1642

It is a for loop. It means there is an element which is repeated x times.

Let's imagine there is this code :

{% for item in forloop %}
    {% if forloop.index <=1 %}
        hello {{item}} !
    {% elsif forloop.index <7 %}
        good bye {{item}} !
    {% else %}
        farewell {{item}} !
    {% endif %}
{% endfor %}

Now let's say forloop variable is an array which content some people names : ['Ava', 'Bob', 'Charly', 'Delta', 'Esp', 'Froll', 'Gosh', 'Hellll']

We could translate that by :

hello Ava ! <!-- forloop.index equals 1 so the `if forloop.index <=1` is executed -->
good bye Bob ! <!-- forloop.index equals 2, so the `if forloop.index <7 executed -->
good bye Charly ! <!-- forloop.index equals 3, so the `if forloop.index <7` is executed -->
good bye Delta ! <!-- forloop.index equals 4, etc... -->
good bye Esp !
good bye Froll !
good bye Gosh !
farewell Hellll ! <!-- forloop.index equals 8, so the `else` is executed -->

Upvotes: 2

Robert Wade
Robert Wade

Reputation: 5003

This is a condition inside a loop meaning :

index is an incremental value indicating which turn in the loop is currently being executed. Usually the first time in the loop is an index of 0.

So based on that the first part of the condition says if the index is less than or equal to 1, then do something.

The second part of the condition is the else if, which would occur if the current loop index was not less than or equal to one, but is still less than 7 (so anywhere from 2-6)

Upvotes: 0

Related Questions