如何在 if 条件中使用过滤器? - shopify 液体

How to use filters inside an if condition? - shopify liquid

是否可以在 if 语句条件中使用过滤器?

除非我创建一个变量来存储过滤后的数据,然后在我的条件下使用它,否则我找不到这样做的方法。我觉得很奇怪,一定有更好的方法。

我想做这样的事情,但出现错误:

{% if numA | plus:5 >= numB %}

我想避免这样做:

{% assign temp = numA | plus:5 %}
{% if temp >= numB %}

您尝试做的事情在 Shopify Liquid 中是不可能的。来自官方 Shopify Liquid 问题页面

Parenthesis aren't allowed in Liquid. They can't be used in conditionals the way you would use them in a programming language.

Filters are not allowed in conditionals, they will lead to unexpected results, at least in Shopify.

The following:

{% if cart.item_count|times:1 > 5 %}

Generates this Liquid warning:

Expected end_of_string but found pipe in "cart.item_count|times:1 > 5"

所以,唯一可能的解决方案就是您在自己的问题中提出的建议。

{% assign temp = numA | plus:5 %}
{% if temp >= numB %}

Math filters in IF condition - Liquid