Skip to content

Request for suggestions: Make it easier to append and prepend items in arrays #1851

@karreiro

Description

@karreiro

Author: Shopify
Expected end date: December 5, 2024

Background

Handling arrays in Liquid is challenging.

Developers often need to convert arrays into strings, perform operations, and then split them back, relying heavily on the Liquid String API as it's more robust than the Array API.

{% assign titles = collections.last.products | map: "title" %}

{{
  titles
  | join: ","
  | append: ",default"
  | split: ","
}}
{{
  titles
  | join: ","
  | prepend: "All,"
  | split: ","
}}

Proposal

We should eliminate workarounds like string concatenation and splitting and make it easier to perform common tasks such as adding new items in an array. This will make Liquid templates simpler, less verbose, and more declarative.

To make that happen, we should support the append and prepend filters in arrays. They will return a new array with the specified element added to the end or beginning, respectively.

{% assign titles = collections.last.products | map: "title" %}

{{ titles | append: "default" }}
{{ titles | prepend: "All" }}

Limitations

This proposal is not backward compatible [1] [2].

{{ titles | append: "new" }}
{% # => "[\"Example T-Shirt\", \"Classic Varsity Top\", \"Classic Leather Jacket\", \"Dark Denim Top\"]new" %}

Therefore, the implementation is pending an impact assessment. If we identify it's impossible to handle backward compatibility, we aim to introduce only the append functionality instead, using an unused filter name (e.g. add).

We're proposing the append/prepend direction first because we consider this a more idiomatic and cohesive direction for the Liquid API.

Call for suggestions

We welcome any feedback or opinions on this proposal. Please share your thoughts by December 5, 2024. Your input is valuable as we prepare to begin active development on this initiative.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions