forloop.index作为shopify中if语句的条件

Tob*_*obi 4 for-loop if-statement liquid shopify

我正在尝试做一些非常简单的事情.我写:

{% for product in collection.products %}
   {{ if forloop.index = 1 }}
        Hello World!
   {{ endif }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

问题: "Hello World!" 出现在每次迭代中.这有什么不对?

Uto*_*Ltd 12

看起来在循环的每一轮中,你覆盖索引总是等于1.试试

{% if forloop.index == 1 %}
Run Code Online (Sandbox Code Playgroud)

代替.

  • 您还应将{{if}}等更改为{%if%},依此类推.双括号用于输出. (2认同)