【问题标题】:forloop.index as a condition in an if-statement in shopifyforloop.index 作为 shopify 中 if 语句中的条件
【发布时间】:2013-04-14 06:13:12
【问题描述】:

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

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

问题: “你好世界!”每次迭代都会出现。 这里有什么问题?

【问题讨论】:

    标签: if-statement for-loop shopify liquid


    【解决方案1】:

    所以这里再次正确的版本,回答我自己的问题... ;)

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

    【讨论】:

      【解决方案2】:

      看起来在循环的每一轮中,您都将索引覆盖为始终等于 1。尝试

      {% if forloop.index == 1 %}
      

      改为。

      【讨论】:

      • 如果您希望它在第一次迭代中发生,您可以尝试 {{ if forloop.first }}
      • 您还应该将您的 {{ if }} 等更改为 {% if %} 等等。双括号用于输出。
      • wiki.shopify.com/Liquid#Tables 的示例看起来与您正在做的事情有点相似。
      猜你喜欢
      • 2019-06-02
      • 2016-09-07
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      相关资源
      最近更新 更多