Support for Infix Operators in Liquid
Overview
This PR introduces a significant syntax improvement to Liquid templates:
Infix Operator Support: More intuitive mathematical and logical operations
This change makes templates more readable and concise by allowing developers to write expressions in a more natural way.
Changes
Infix Operator Support
- Add arithmetic operators:
+,-,*,/ - Add comparison operators:
>,>=,==,<=,< - Add logical operators:
&&,|| - Add XOR operator:
^ - Support operator precedence and parenthesized expressions
Before:
{% assign media_count = media_count | minus: variant_images.size | plus: 1 %}
After:
{% assign media_count = media_count - variant_images.size + 1 %}
Benefits
- More Readable Code: Expressions are written in a more natural and familiar syntax
- Less Verbose: Fewer characters and more straightforward expressions
- Easier Maintenance: Simpler syntax reduces cognitive load for developers
- Familiar Syntax: Follows conventions from other programming languages
Testing
Added a new test suite:
infix_operators_unit_test.rb: Tests arithmetic, comparison, and logical operators
All tests should pass with the implementation.
Split this out from https://github.com/Shopify/liquid/pull/1922 because the work is massively more complex than is feasible to prioritize at this time. These unit tests can still be useful when we return to this.
Liquid being postfix-y (with filter chaining and such), introducing infix feels as seismic as the Shopify Admin API going GraphQL.
This isn't good or bad, just a, uh, premonition. ;)