液体控制流标签{%for%}和{%else%}

时间:2018-01-17 16:01:37

标签: shopify liquid control-flow

在Shopify主题中使用液体还是新手,并且在一些领域缺乏文档,所以我在"随时随地学习"模式,我从头开始重建Shopify的简单主题作为学习工具。我在collection-template.liquid中找到了这段代码:

1   {% for product in collection.products %}
2       {% if collection.products.size == 1 %}
3           <!-- Template Logic -->
4       {% else %}
5           <!-- Template Logic -->
6       {% endif %}
7           
8       {% include 'product-grid-item' %}
9
10  {% else %} <!-- HANGING ELSE STATEMENT? -->
11
12      <!-- Template Logic -->
13
14      {% if shop.products_count == 0 and collection.handle == 'all' %}
15          <!-- Template Logic -->
16      {% else %}
17          <!-- Template Logic -->
18      {% endif %}
19  {% endfor %}

来自Java背景,第10行看起来像编译错误。它是一个else语句,没有开放if

但是,根据上下文线索,我想知道{% else %}是否像if (empty)一样,这意味着上述代码段在功能上等同于:

{% if collection.products.size == 0 %}
    <!-- Line 11-18 from above snippet -->
{% else %}
    {% for product in collection.products %}
        <!-- Line 2-9 from above snippet -->
    {% endfor %}
{% endif %}

有人可以证实吗?

1 个答案:

答案 0 :(得分:2)

对于for循环使用的集合长度为零的情况,这是一个后备。见https://help.shopify.com/themes/liquid/tags/iteration-tags#else