Request for suggestions: Make it easier to `append` and `prepend` items in arrays
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 11.9k
- Forks
- 1.5k
- Avg merge
- 17h 55m
- Merged PRs (30d)
- 3
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with lib/liquid/standardfilters.rb at the linked lines 621–623 and 650–652, then assess how append and prepend currently behave for arrays and strings. Done means determining whether backward compatibility permits the proposed filters, or recommending the alternative unused filter name if it does not.
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
- 35/100