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

Comparisons within assign #1102

Open
andymikulski opened this issue May 15, 2019 · 3 comments
Open

Comparisons within assign #1102

andymikulski opened this issue May 15, 2019 · 3 comments

Comments

@andymikulski
Copy link

andymikulski commented May 15, 2019

It appears that the assign operator doesn't like comparisons (i.e. [thing] contains [other thing]). For instance, I would expect this to work:

{%- assign is_some_product = variant.tags contains "example" -%}

But produces the error: Expected end_of_string but found comparison in [...]

I've used the following as a workaround:

{% if variant.tags contains "example" %}
  {%- assign is_some_product = true -%}
{% else %}
  {%- assign is_some_product = false -%}
{% endif %}

While the latter works, the former is much more concise and is what I would expect to work. Note that I found this while trying to use the or operator as well, but seeing as comparisons themselves aren't accepted, I don't think the parser even got that far.


edit 10/30/2020: The following snippet also works and is a bit more compact:

{%- assign is_some_product = false -%}
{% if variant.tags contains "example" %}
  {%- assign is_some_product = true -%}
{% endif %}
@andymikulski andymikulski changed the title Conditional operators with assign Comparisons within assign May 15, 2019
@shopmike
Copy link
Contributor

shopmike commented Oct 3, 2019

Hey, @andymikulski I agree this is would be a cool addition to Liquid. There are a lot of things we are currently working on but would love to think this will make it in one day.

If someone is willing to take a crack at a pull request in the meantime I'm happy to review it.

For a bit of direction
https://github.com/Shopify/liquid/blob/master/lib/liquid/tags/assign.rb#L25
Variable.new needs to be able able to handle Conditions
https://github.com/Shopify/liquid/blob/master/lib/liquid/condition.rb

@torian257x
Copy link

torian257x commented Aug 13, 2021

one wonders a bit what went wrong that this isn't standard... security is the issue I assume?

@msangel
Copy link

msangel commented Mar 17, 2023

@shopmike wrote:

Variable.new needs to be able able to handle Conditions

The same should be applied to printing out variables: {{ 99 > 98 }} should return true or false or anything better than 99 in lax mode and an exception in strict mode.
Related report: bkiers/Liqp#267

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

4 participants