Skip to content

Conversation

@jg-rp
Copy link
Owner

@jg-rp jg-rp commented May 21, 2025

This PR implements compound expressions and arithmetic operators.

Compound expression contain logical operators (and and or) and/or relational operators (==, >, etc.). They can appear in most places where a primitive expressions would previously have been used, including in output statements, in assign tags and as arguments to filters.

See Shopify/liquid#1922 and jg-rp/liquid#175.

For example, we can now write this:

{{ user.name or "guest" }}

Whereas before we'd need an {% if %} tag.

{% if user.name -%}
  {{ user.name }}
{%- else -%}
   guest
{%- endif %}

Arithmetic infix operators +, -, *, /, % and **, and prefix operators + and -, are disabled by default. Enable them by setting arithmetic_operators to True in an Environment subclass.

from liquid2 import Environment

class MyEnvironment(Environment):
    arithmetic_operators = True

env = MyEnvironment()
template = env.from_string("{{ a + b * c }}")
print(template.render(a=1, b=2, c=3))  # 7

Without arithmetic operators, the equivalent expression would be {{ b | times: c | plus: a }}.

@jg-rp jg-rp changed the title Implement short circuit evaluation and arithmetic operators Implement compound expressions and arithmetic operators May 21, 2025
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.

2 participants