Tobi
Tobi

Reputation: 192

forloop.index as a condition in an if-statement in shopify

I'm trying to do something very simple. I wrote:

{% for product in collection.products %}
   {{ if forloop.index = 1 }}
        Hello World!
   {{ endif }}
{% endfor %}

Problem: "Hello World!" appears in every iteration. What is wrong here?

Upvotes: 8

Views: 48962

Answers (3)

Sandrik85
Sandrik85

Reputation: 49

Use native Liquid check

    
    {% for product in collection.products %}
        {% if forloop.first %}
            Hello World!
        {% endif %}
    {% endfor %}

Upvotes: 2

Tobi
Tobi

Reputation: 192

So here again the right version, answering my own question... ;)

{% for product in collection.products %}
   {% if forloop.index == 1 %}
        Hello World!
   {% endif %}
{% endfor %}

Upvotes: 2

UtopiaLtd
UtopiaLtd

Reputation: 2590

Looks like in each round of the loop, you're overwriting the index to always be equal to 1. Try

{% if forloop.index == 1 %}

instead.

Upvotes: 31

Related Questions