Skip to content
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

Feature Request: Loop variable inside map filter #3627

Open
alexander-schranz opened this issue Jan 13, 2022 · 1 comment
Open

Feature Request: Loop variable inside map filter #3627

alexander-schranz opened this issue Jan 13, 2022 · 1 comment

Comments

@alexander-schranz
Copy link
Contributor

I really like that twig have a loop variable in its for loops.

It would be great to also have then in a loop with the the map filter. For example:

{% set options = (choices|map((choice) => {
   checked: (loop.first and value) and choice.value in value,
})) %}

https://twigfiddle.com/4ntrof

@alexander-schranz alexander-schranz changed the title Loop variable inside map Loop variable inside map filter Jan 13, 2022
@alexander-schranz alexander-schranz changed the title Loop variable inside map filter Feature Request: Loop variable inside map filter Jan 13, 2022
@m-vo
Copy link

m-vo commented Apr 2, 2022

IMHO at least for array_map there is no defined order in which the callback function should be called (although the implementation uses the canonical order). From a language design perspective, this is a good thing, because then you can potentially parallelize execution.

What about instead implementing a function that enumerates an array (inspired by Python)? This would force canonical keys or wrap elements as ['loop0' => $index, 'value' => $element]:

choices|enumerate|map((value, index) => …)
choices|enumerate|map(e => e.value…)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants