liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Support for Boolean Expressions in Liquid

Open albchu opened this issue 9 months ago • 6 comments

Overview

This PR introduces a syntax improvement to Liquid templates:

Boolean Expression Support: Enhanced boolean handling with proper operator precedence

This change makes templates more readable and concise by allowing developers to write expressions in a more natural way.

Changes

Boolean Expression Enhancements

  • Add support for logical operators (and, or)
  • Implement proper operator precedence (e.g., false and false or true evaluates to true)
  • Allow direct boolean expressions in assignment statements

Before:

{%- liquid
  assign lazy_load = false
  if media_position > 1
    assign lazy_load = true
  endif
%}

After:

{% assign lazy_load = media_position > 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:

  • boolean_unit_test.rb: Tests boolean operators, precedence, and assignment

All tests should pass with the implementation.

albchu avatar Mar 04 '25 20:03 albchu

yessssssss

t-kelly avatar Mar 04 '25 20:03 t-kelly

Now this is something to be excited for. Top-tier branch name too.

image

chrisberthe avatar Mar 04 '25 20:03 chrisberthe

ooo, is a goal here (explicit or implicit) to deepen the functionality of if and unless? via parity for comparisons across the board?

as in,

{% if false and (false or true) %} {%# not supported in main %}
{% if false and false or true %} {%# does not pass in main %}
{% if false || true %} {%# not supported in main %}

isaacbowen avatar Mar 05 '25 18:03 isaacbowen

ooo, is a goal here (explicit or implicit) to deepen the functionality of if and unless? via parity for comparisons across the board?

Hey @isaacbowen, sort of! The current focus is to enable more concise boolean statements in output markup. So previously all falsey values would render as nothing. We'd like to support syntax where if var_1 = true and var_2 = false, we can evaluate {{ var_1 and var_2 }} to "false".

albchu avatar Mar 05 '25 23:03 albchu

This looks like a simplified version of the Ternary operator feature request Would it maybe be possible to look into that, as it covers more usecases?

madsenmm avatar Mar 06 '25 19:03 madsenmm

yes, yes, yes! and thumbsup for @madsenmm this could be the next step for this.

efekurnaz avatar Mar 07 '25 12:03 efekurnaz