How to use multiple arguments in an if statement with Liquid

♀尐吖头ヾ 提交于 2019-11-27 12:43:30

问题


I want to use an if statement in Liquid with multiple conditionals. Something like:

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

Multiple conditionals don't seem to work. Have I got the syntax wrong or can Liquid not handle this sort of if statement?


回答1:


Unfortunately, Liquid has a poor implementation of boolean algebra.

Using Liquid's operators and tags, here is a dirty way to achieve it:

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



回答2:


Another way you can condense this is to combine else if statements, and booleans don't necessarily need the "==" when evaluating true:

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


来源:https://stackoverflow.com/questions/23054564/how-to-use-multiple-arguments-in-an-if-statement-with-liquid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!