-
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
Fix for loop with reversed and limit. #456
base: main
Are you sure you want to change the base?
Conversation
A (temporary) fix in your liquid templates would be:
|
Hm. Good catch. I agree that it makes more sense like this. But this fix is not strictly backwards compatible... @Shopify/liquid, think we can ship this without breakage? |
@@ -80,6 +80,8 @@ def render(context) | |||
context[@attributes['offset'.freeze]].to_i | |||
end | |||
|
|||
collection.reverse! if @reversed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an in place operation on the collection itself, which seems like it could have some nasty side effects. Also, the collection may not be an array at this point, since Utils.slice_collection
supports anything with the method each
(see Liquid::Utils.slice_collection_using_each).
We could log a warning when a |
There is an argument to be made here that
and
should not behave the same way (one should return [3,2], the other should return [2,1]) |
@fw42 The current syntax does not support the |
Good point :-) Can you rebase this PR? |
In that case, since the syntax is |
true :) |
Then maybe lets just implement the new syntax as well? |
👍, addresses the original issue and doesn't require verifying templates don't break. |
It is the other way around. Looking at the test I wrote for the PR (already some time ago :)) the behaviour previous to my PR was weird: |
So the syntax it supports is the first version in @fw42's earlier comment, not the second. |
Yep. |
@pushrax: What should we do here? Add some logging to Shopify first to see if we can ship this without breakage? It's not backwards compatible. |
Could do this with static analysis – I need to get the harness set up for that again anyway (for the formatter and #569) so maybe that's the best course of action? |
@pushrax: Any intentions of still doing this? |
Not in the short term. |
I have no idea what it would mean to do this, but it seems that if you let
Here, "reversed" and "limit:2" are effectively tags on the loop, so that keeps the current behavior. By comparison:
filters the collection we iterate over, so the loop itself remains ignorant of the situation. This means adding a filter that takes the top n items of an array, which I'd like Liquid to add, anyway. How feasible would this be, given the current design? We could start with just adding a "top" filter, and in the worst case, we'd have
I'd be thrilled with that. Even a |
@pushrax @dylanahsmith I'm curious about the status of this PR. I would love to see the parameters execute as expected, from left to right ( As a compromise, would it be possible to optionally add a value to
|
currently returns [2,1]. This fix makes it more logically: [3,2].