liquid icon indicating copy to clipboard operation
liquid copied to clipboard

[Idea] Introduce concept of sub-expression

Open bakura10 opened this issue 3 years ago • 0 comments

Hi,

This idea would be linked to #1530 to make the language easier to use.

Right now, Liquid does not allow creating variable or having logic in tag. This means the following is not possible:

{% render 'color-swatch', selected: forloop.first or product.selected_or_first_available_variant.matched %}

This forces developer to create temporary variable:

{% if forloop.first or product.selected_or_first_available_variant.matched %}
  {% assign selected = true %}
{% endif %}

{% render 'color-swatch', selected: selected %}

This is not only more verbose but introduce hard to spot bugs, because if this code is within a for loop, it will be set to true on first iteration, and stay like this on second, third... iteration, so it should be like this:

{% assign selected = false %}

{% if forloop.first or product.selected_or_first_available_variant.matched %}
  {% assign selected = true %}
{% endif %}

{% render 'color-swatch', selected: selected %}

The more condition you add, the more local variables you need to create, with all the potential side-effects.

It would be awesome if Liquid could support the ability to create variable or apply filter inline, for instance by using such notation with parenthesis (because this is not allowed right now this would not be a BC):

{% render 'color-swatch', selected: (forloop.first or product.selected_or_first_available_variant.matched) %}
{% render 'button', link: (section.settings.link_url | default: collection.url) %}

This would also open the door for more expressive syntax like this:

{% if (option.value | downcase) == 'color' %}
  // Do something
{% endif %}

bakura10 avatar Mar 26 '22 04:03 bakura10