在 Liquid 模板中调用数组中的项目时,如何调用
does not contain
或 not in array
?
unless
来救援!
创建一个 [A, B, C] 数组。
{% assign input = "A,B,C" | split:"," %}
unless
仅在不满足约束时打印。
这不会打印任何内容:
{% unless input contains 'A' %}No A{% endunless %}
这将打印“No Z”:
{% unless input contains 'Z' %}No Z{% endunless %}
你可以这样做:
{% if collection.tags contains 'tag' %}
{% else %}
do stuff!
{% endif %}
我只是想在集合/搜索结果页面中循环抛出产品变体并显示当前变体格式,因此在
product-grid-item.liquid
文件中。在我们的网上商店中,某些格式有两种颜色版本,因此我必须只显示一种格式。
我也尝试了Lucas的回答,但没有成功,不明白,为什么。
所以我解决了另一种方法:在循环之前声明一个字符串并将其值设置为当前的变体格式。检查下一个变体格式是否不是之前的值。
{% assign currentFormat = '' %}
{%- for variant in product.variants -%}
{%- if currentFormat != variant.title -%}
<div>{{ variant_title }}</div>
{% assign currentFormat = variant.title %}
{%- endif -%}
{%- endfor -%}