Shopify / Shopify/liquid

Feature request: Better array functions

Open
#1,202 4 comments 10 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

Array creation and manipulation in Liquid is quite difficult as it currently stands. The method I see promoted everywhere to create arrays is to combine strings, then use the split function, but in situations where a non-string value is required you end up having to coerce a null value into an array using a method such as null | sort, followed by concatenating the values you actually want into said array.

I imagine that nothing can really be done to allow users to create arrays directly, but perhaps a more descriptive function would suit, such as {% assign array = null | to_array %}? In this case, it would return an empty array, but any non-null value would become the first array value.

If you were looping through a collection and all products needed to be pushed to a new array, you might write something like {% assign products = product | to_array: products %}, which would push the product to the end of the products array if it already exists, but create an array with product as the first value if it doesn't already exist.

Since we can already make arrays, they just require awkward methods to work with, I think it would make sense to allow users to do something like this. It would cut back on boilerplate code.

One thing my proposal doesn't address though is the ability to create nested arrays. Since we can't build objects, arrays at the very least give us one method of controlling how our data is structured, but nested arrays would provide even more flexibility and as such I hope that they can be considered too.

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

No files, tests, or entry points are named. Start by locating the existing array creation and manipulation behavior, then clarify the proposed to_array semantics for null, non-null, appending, and nested arrays; done means the API is agreed and covered by tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.