Support for Boolean Expressions in Liquid
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 trueevaluates totrue) - 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.

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

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 %}
ooo, is a goal here (explicit or implicit) to deepen the functionality of
ifandunless? 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".
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?
yes, yes, yes! and thumbsup for @madsenmm this could be the next step for this.