Issue
Is it possible to use filters inside an if statement condition ?
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 %}
Solution
What you are trying to do in not possible in Shopify Liquid. From the official Shopify Liquid issues page
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"
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
Answered By - Bilal Akbar Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.