Reputation: 192
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
Reputation: 49
Use native Liquid check
{% for product in collection.products %} {% if forloop.first %} Hello World! {% endif %} {% endfor %}
Upvotes: 2
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
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