Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Boolean Expressions in Liquid #1922

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

albchu
Copy link

@albchu albchu commented Mar 4, 2025

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.

@t-kelly
Copy link

t-kelly commented Mar 4, 2025

yessssssss

@chrisberthe
Copy link

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

image

@albchu albchu changed the title Support for Boolean Expressions and Infix Operators in Liquid Support for Boolean Expressions in Liquid Mar 4, 2025
@albchu albchu force-pushed the better-bools-than-ghouls branch from 5bba11e to a2de2dc Compare March 4, 2025 23:59
@isaacbowen
Copy link

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 %}

@albchu
Copy link
Author

albchu commented Mar 5, 2025

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".

@madsenmm
Copy link

madsenmm commented Mar 6, 2025

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?

@efekurnaz
Copy link

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

@albchu albchu force-pushed the better-bools-than-ghouls branch from 756936a to 08d36b0 Compare March 11, 2025 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants