Shopify / Shopify/liquid

[Idea] Introduce concept of sub-expression

Open
#1,559 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

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 %}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the proposed render, assign, if, and filter examples in this issue, then review linked issue #1530 for related requirements. Define the supported sub-expression forms, parsing behavior, and compatibility expectations before implementation; done means the examples have clear, tested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.