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

See*_*ker 5 if-statement filter liquid shopify

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

我发现没有办法做到这一点,除非我创建一个变量来存储过滤后的数据,然后在我的条件中使用它。我觉得很奇怪,一定有更好的方法。

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

{% if numA | plus:5 >= numB %}
Run Code Online (Sandbox Code Playgroud)

我想避免这样做:

{% assign temp = numA | plus:5 %}
{% if temp >= numB %}
Run Code Online (Sandbox Code Playgroud)

Bil*_*bar 6

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

Liquid 中不允许使用括号。它们不能像在编程语言中那样在条件语句中使用。

条件中不允许使用过滤器,它们会导致意外结果,至少在 Shopify 中是这样。

下列:

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

生成此液体警告:

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

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

{% assign temp = numA | plus:5 %}
{% if temp >= numB %}
Run Code Online (Sandbox Code Playgroud)

IF 条件下的数学滤波器 - 液体