Shopify / Shopify/liquid

[request] make arrays in liquid / concat filter

Open
#699 2 comments 2 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

Problem

  • Not being able to make arrays in liquid.
  • Simple tasks end up requiring multiple server requests for information.
  • Memory limit is often exceeded when using workarounds.

Context

  • This is often seen when nesting for loops in template files (i.e. looping through articles then looping through article tag).
  • Because Shopify doesn't offer a way to build arrays, we can't optimize to one for loop to create arrays.

https://screenshot.click/08-51-bbzg1-xi7ej.png

Possible Solution

  • The concat filter seems to be a possible solution. We can see that concat is concatenating hashes. If we could create arrays with this it would save a lot of requests from hitting the server.
Example of concat hashes
Ideal fix

https://screenshot.click/08-57-txwh6-s16ob.png

Current situation:
{% for article in blogs[settings.blog_widget_select].articles %}
   {% for tag in article.tags %}
      {% assign current_tag = tag | downcase %}
      {% if current_tag == 'tag 1' %}
         // print article
      {% endif %}
   {% endfor %}                     
{% endfor %}


{% for article in blogs[settings.blog_widget_select].articles %}
   {% for tag in article.tags %}
      {% assign current_tag = tag | downcase %}
      {% if current_tag == 'tag 2' %}
         // print article
      {% endif %}
   {% endfor %}                     
{% endfor %}

{% for article in blogs[settings.blog_widget_select].articles %}
   {% for tag in article.tags %}
      {% assign current_tag = tag | downcase %}
      {% if current_tag == 'tag 3' %}
         // print article
      {% endif %}
   {% endfor %}                     
{% endfor %}

{% for article in blogs[settings.blog_widget_select].articles %}
   {% for tag in article.tags %}
      {% assign current_tag = tag | downcase %}
      {% if current_tag == 'tag 4' %}
         // print article
      {% endif %}
   {% endfor %}                     
{% endfor %}
Ideal situation:
{% assign tag_1_articles = false %}
{% assign tag_2_articles = false %}
{% assign tag_3_articles = false %}
{% assign tag_4_articles = false %}

{% for article in blogs[settings.blog_widget_select].articles %}
   {% for tag in article.tags %}
      {% assign current_tag = tag | downcase %}
      {% if current_tag == 'tag 1' %}
         {% assign tag_1_articles = tag_1_articles | concat:article %}
      {% endif %}

      {% if current_tag == 'tag 2' %}
         {% assign tag_2_articles = tag_2_articles | concat:article %}
      {% endif %}

      {% if current_tag == 'tag 3' %}
         {% assign tag_3_articles = tag_3_articles | concat:article %}
      {% endif %}

      {% if current_tag == 'tag 4' %}
         {% assign tag_4_articles = tag_4_articles | concat:article %}
      {% endif %}
   {% endfor %}                     
{% endfor %}

// print tag_1_articles

// print tag_2_articles

// print tag_3_articles

// print tag_4_articles

CC: @NathanPJF

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

Read lib/liquid/standardfilters.rb around line 218 to understand the existing concat behavior, then examine how Liquid represents arrays and hashes in filters. The work is done when a documented, tested approach lets concat build arrays as shown in the issue, or the supported alternative is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
web-dev
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.