在液体模板中搜索数组中的匹配项时,如何调用contains exactly
?例如,如果网页的标记可能包含separable
或non-separable
,那么如何找到仅包含separable
而非non-separable
标记的网页?根据我的经验,{% if post.tags contains 'separable' %}
语句会考虑这两种情况。
答案 0 :(得分:2)
请参阅 documentation ,您可以使用此过滤器
{% assign tags = post.tags | where:"tag","separable" %}
答案 1 :(得分:1)
循环遍历数组并使用匹配运算符检查值。如果匹配则将变量从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 %}