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

Allow arrow functions everywhere #4378

Merged
merged 1 commit into from
Oct 8, 2024
Merged

Conversation

fabpot
Copy link
Contributor

@fabpot fabpot commented Oct 4, 2024

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.

src/ExpressionParser.php Outdated Show resolved Hide resolved
src/ExpressionParser.php Outdated Show resolved Hide resolved
doc/templates.rst Outdated Show resolved Hide resolved
doc/templates.rst Outdated Show resolved Hide resolved
doc/templates.rst Outdated Show resolved Hide resolved
@fabpot fabpot force-pushed the arrow-everywhere branch 2 times, most recently from 362420a to a3ebb78 Compare October 7, 2024 06:43
@fabpot
Copy link
Contributor Author

fabpot commented Oct 7, 2024

Rebased

@fabpot fabpot merged commit 9b35289 into twigphp:3.x Oct 8, 2024
48 of 49 checks passed
@fabpot fabpot deleted the arrow-everywhere branch October 8, 2024 07:29
@Lorenzschaef
Copy link
Contributor

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:

<a href="{{ buildUrl(document) }}">

My workaround that does work:

<a href="{{ [document]|map(buildUrl)|first }}>

Are there any plans to support this?

@stof
Copy link
Member

stof commented Dec 22, 2024

The way to call it (in 3.17.1 and newer) is buildUrl.__invoke(document)

Twig does not support using buildUrl(document) as this would be ambiguous whether buildUrl refers to a Twig function or a Twig variable (forcing to remove all validation of function existence from the compile time of templates)

@Lorenzschaef
Copy link
Contributor

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 invoke filter would be a good solution to this.

@fabpot
Copy link
Contributor Author

fabpot commented Dec 26, 2024

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 invoke filter would be a good solution to this.

Having a dedicated filter makes sense to me. Would you like to work on this @Lorenzschaef ?

@Lorenzschaef
Copy link
Contributor

@fabpot I can do that, adding the filter shouldn't be hard.

@Lorenzschaef Lorenzschaef mentioned this pull request Dec 26, 2024
fabpot added a commit that referenced this pull request Jan 12, 2025
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

How to use arrow functions as callable? Arrow function are not allowed outside filters
4 participants