-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Allow arrow functions everywhere #4378
Conversation
dbb38b7
to
f0909ce
Compare
f0909ce
to
636e3d7
Compare
362420a
to
a3ebb78
Compare
a3ebb78
to
aca4d22
Compare
Rebased |
I updated twig for this feature. However, I found that there is one piece missing, to what I was actually looking for. I can store an arrow function in a variable and pass it around, but I can't call it. My use case: I have a reusable component/partial, that displays a list of documents as links. Depending on the context, I want the link's URL to be different, so I need to pass a function that generates the link for each item. What I would expect to work:
My workaround that does work:
Are there any plans to support this? |
The way to call it (in 3.17.1 and newer) is Twig does not support using |
Ok, it makes sense to not allow the normal function call syntax then. But isn't the fact that arrow functions have an __invoke() mehtod an (undocumented) implementation detail? I wouldn't really feel comfortable relying on that in my code. I think an |
Having a dedicated filter makes sense to me. Would you like to work on this @Lorenzschaef ? |
@fabpot I can do that, adding the filter shouldn't be hard. |
This PR was merged into the 3.x branch. Discussion ---------- Add invoke filter Adds a new filter `invoke`, to invoke arrow functions and other php callables. See the discussion here: #4378 (comment) Commits ------- 3e93e91 Finish the work c4fd25f fix indentation dacbe51 Apply suggestions from code review 2b95bd6 typo 09c30c3 fix merge d58ac9a docs and changelog edecc13 typehint instead of checkArrow f271158 checkArrow, typehints 8b43275 invoke filter
Closes #3192
Closes #3402
Arrow functions are now supported for all Twig callables (filters, functions, and tests), macro arguments, and method call arguments.
Arrow functions can also be stored in variables.