-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request for suggestions: Make it easier to append
and prepend
items in arrays
#1851
Comments
Would lack of backwards-compatibility not break any existing theme that’s treating append as a string? Will there be any way to determine which version of liquid a theme uses? Would it not make more sense to use |
Just so you know, you can use the following syntax to append elements in an array: {% liquid
assign string_list = "second,third,fourth" | split: ','
assign string_to_prepend = "first"
assign string_to_append = "fifth"
# Prepending to a Liquid array
assign string_list = string_to_prepend | uniq | concat: string_list
# Appending to a Liquid array
assign string_list = string_list | reverse
assign string_list = string_to_append | uniq | concat: string_list | reverse
echo string_list | append: '<br>'
# => ["first", "second", "third", "fourth", "fifth"]
%} Sure, dedicated filters would be nice but I wouldn't call Array API inconsistent or less robust. It is simply under-explored by most Shopify developers I agree with @galenking, |
I think I prefer |
Adding element to the end of array: {{ titles | push: 'new_title' }} Adding element at the beginning of an array: {{ 'new_title' | push: titles }} Edit: Since we already know |
+1 on arrays better support for an array type
|
would love better array support. using existing keywords doesnt make sense to me. the functionality is different, a different keyword makes sense. plus as mentioned backwards compatibility issues would cause massive problems for custom dev shops |
👋 Hey everyone, Thanks for the feedback! One constraint we have is that we need to keep the original array immutable in Liquid. Thus, we did consider the Currently, we're considering Maybe the assumption that Thanks again, everyone, for the feedback! |
I agree, Modifying the Edit: I've just seen you've already mentioned using |
A little late to the party, and honestly I don't have anything groundbreaking or insightful to offer that everyone else has. I am a big user of the app DataJet, and they fork Liquid and have added some really easy to use filters and tags I am massively fond of. Maybe instead adding more functionality to the existing filters which could cause backwards compatibility issues, maybe a new tag could be used? I don't know if their documentation and examples help with any inspiration or if it's just wasting time to even look at, but I'm just throwing them out into the wind: https://docs.datajet-app.com/liquid/tags/push |
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.
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
andprepend
filters in arrays. They will return a new array with the specified element added to the end or beginning, respectively.Limitations
This proposal is not backward compatible [1] [2].
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.
The text was updated successfully, but these errors were encountered: