Liquid 中有没有办法使用 contains 运算符来检查精确匹配?

Is there a way in Liquid to use the contains operator to check for an exact match?

在 Liquid 模板中搜索数组匹配时,如何调用 contains exactly?例如,如果一个页面的标签可能包含 separablenon-separable,您如何找到只包含 separable 而没有 non-separable 标签的页面?根据我的经验,{% if post.tags contains 'separable' %} 语句考虑了这两种情况。

遍历数组并使用匹配运算符检查值。如果它匹配将变量从 false 更改为 true:

{% assign found_seperable = false %}
{% for tag in post.tags %}
  {% if tag == 'separable' %}
    {% assign found_seperable = true %}
  {% endif %}
{% endfor %}

然后检查变量:

{% if found_seperable %}
  do what you want if true
{% else %}
  do what you want if false
{% endif %}

参考documentation你可以使用这个过滤器

{% assign tags = post.tags | where:"tag","separable" %}