如何在带有Liquid的if语句中使用多个参数

时间:2014-04-14 07:40:27

标签: if-statement jekyll liquid

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

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

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

2 个答案:

答案 0 :(得分:42)

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

使用Liquid的operatorstags,这是实现它的一种肮脏方式:

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

答案 1 :(得分:10)

另一种可以压缩的方法是结合使用if语句,而布尔值在评估true时不一定需要“==”:

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