如何在Liquid中使用多个参数进行if语句判断

38

我想在Liquid中使用带有多个条件的if语句。类似于:

{% if (include.featured == "true" and product.featured == "true") or (include.featured == "false" and product.featured == "false") %}

多个条件语句似乎不起作用。是我语法错了还是Liquid不能处理这种if语句?


2
在您的特定示例中,您可以只需使用 {% if include.featured == product.featured %} - Devon Parsons
2个回答

47

不幸的是,Liquid 的布尔代数实现较差。

使用 Liquid 的 操作符标签,以下是一种实现方法:

{% if include.featured == true and product.featured == true %}
      {% assign test = true %}
{% endif %}

{% if include.featured == false and product.featured == false %}
      {% assign test = true %}
{% endif %}

{% if test %}
      Yepeeee!
{% endif %}

你也可以使用 if/elsif 来保持所有内容的一致性。 - Owen
1
'||' 也可以表示为 'or' :~) - dawn

11

另一种简化的方法是将else if语句合并,并且在评估为真时,布尔值不一定需要使用"==":

{% if include.featured and product.featured %}
      {% assign test = true %}
{% elsif include.featured == false and product.featured == false %}
      {% assign test = false %}
{% endif %}

3
如果是用Jekyll的话,"elseif"需要拼成"elsif"。否则这段信息对我很有帮助。 - Ron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接