我想在Liquid中使用带有多个条件的if
语句。类似的东西:
{% if (include.featured == "true" and product.featured == "true") or (include.featured == "false" and product.featured == "false") %}
多个条件似乎不起作用。我的语法错误了还是Liquid不能处理这种if语句?
答案 0 :(得分:42)
不幸的是,Liquid的布尔代数实现很差。
使用Liquid的operators和tags,这是实现它的一种肮脏方式:
{% 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 %}