Liquid 有“不包含”或“不在数组中”运算符吗?

问题描述 投票:0回答:3

在 Liquid 模板中调用数组中的项目时,如何调用

does not contain
not in array

jekyll liquid
3个回答
116
投票

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 %}

17
投票

你可以这样做:

{% if collection.tags contains 'tag' %}
{% else %}
  do stuff!
{% endif %}

0
投票

我只是想在集合/搜索结果页面中循环抛出产品变体并显示当前变体格式,因此在

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 -%}
© www.soinside.com 2019 - 2024. All rights reserved.