简体   繁体   English

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

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

Is it possible to use filters inside an if statement condition?是否可以在 if 语句条件中使用过滤器?

I found no way of doing this unless I make a variable that stores the filtered data and then use it inside my condition.我发现没有办法这样做,除非我创建一个存储过滤数据的变量,然后在我的条件中使用它。 Seems odd to me, there must be a better way.对我来说似乎很奇怪,必须有更好的方法。

I want to do something like this but I am getting an error:我想做这样的事情,但我收到一个错误:

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

I want to avoid doing this:我想避免这样做:

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

What you are trying to do in not possible in Shopify Liquid.在 Shopify 液体中,您尝试做的事情是不可能的。 From the official Shopify Liquid issues page来自官方 Shopify 液体问题页面

Parenthesis aren't allowed in Liquid. 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.条件中不允许使用过滤器,它们会导致意想不到的结果,至少在 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"

So, the only possible solution is what you suggested in your own question.因此,唯一可能的解决方案是您在自己的问题中提出的建议。

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

Math filters in IF condition - Liquid IF 条件下的数学过滤器 - 液体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM