Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 609
Tobi Avatar asked Dec 05 '22 11:12

Tobi


2 Answers

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.

like image 173
UtopiaLtd Avatar answered Jan 13 '23 12:01

UtopiaLtd


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

{% for product in collection.products %}
   {% if forloop.index == 1 %}
        Hello World!
   {% endif %}
{% endfor %}
like image 40
Tobi Avatar answered Jan 13 '23 13:01

Tobi