liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Support for Infix Operators in Liquid

Open albchu opened this issue 9 months ago • 2 comments

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.

albchu avatar Mar 04 '25 23:03 albchu

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.

albchu avatar Mar 04 '25 23:03 albchu

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. ;)

isaacbowen avatar Mar 05 '25 18:03 isaacbowen