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

Use filters with expressions #287

Closed
almousa1990 opened this issue Dec 24, 2020 · 2 comments
Closed

Use filters with expressions #287

almousa1990 opened this issue Dec 24, 2020 · 2 comments

Comments

@almousa1990
Copy link

almousa1990 commented Dec 24, 2020

Hi,

For some reasons, filters don't seem to work when evaluating and expressions, for example, the following will always evaluate (with filters):

{% assign people = "alice, bob, carol" | split: ", " -%}
{%- if people | size == 1 -%}shouldn't evaulate{%- endif%}

The following will not evaluate:

{% assign people = "alice, bob, carol" | split: ", " -%}
{%- if people.size == 1 -%}shouldn't evaulate{%- endif%}

Note that both conditions should have the same outcome

@harttle
Copy link
Owner

harttle commented Jan 23, 2021

I tried the Ruby version just to find it doesn't support this too. But I'm happy to have this feature in LiquidJS. I'll keep this open till someone implement it.

harttle pushed a commit that referenced this issue Feb 12, 2021
# [9.23.0](v9.22.1...v9.23.0) (2021-02-12)

### Bug Fixes

* respect `fs` in parser options, for [#233](#233) ([4e82da6](4e82da6))

### Features

* support filters in if/unless/case, see [#287](#287) ([2f059f6](2f059f6))
* support function calls, closes [#222](#222) ([e37824f](e37824f))
* support layout none, closes [#299](#299) ([81e11bb](81e11bb))
@harttle
Copy link
Owner

harttle commented Feb 12, 2021

The operator priority is another potential issue to use filters in expressions.

{{ foo | bar: 'a'  ==  'b' }}  // one may expect == has higher priority
{{ people | size == 1 }}     // one may expect '|' has higher priority

But I managed to make this work (the | always has higher priority, which is more intuitive and compliant to the current implementation):

it('should support filter on expression', async function () {
liquid.registerFilter('negate', (val) => !val)
const src = '{% if 2 == 3 | negate %}yes{%else%}no{%endif%}'
const html = await liquid.parseAndRender(src, scope)
return expect(html).to.equal('yes')
})

Now filters are available for expressions in if, unless and case.

@harttle harttle closed this as completed Feb 12, 2021
@harttle harttle changed the title Filters don't work in expressions Use filters with expressions Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants