-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add null-safe operator #4717
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
base: 3.x
Are you sure you want to change the base?
Add null-safe operator #4717
Conversation
|
This should fix issue #3260 |
|
The failing tests are already present in twigphp:3.x. I have no idea what they refer to, and I don't think they are related to the changes in this PR. |
|
I've found the bug. It was not in this repo, but it appeared when I think testing for exact results from dependencies is not a good idea, since it would require updating the tests in this lib every time the depended package was updated, so I changed the test to expect at least N results instead of exactly N results. |
|
This is a duplicate of #4623 |
|
@willrowe thanks for pointing that out. I wonder why that PR is stuck. It looks pretty good too... |
|
up |
Adds native support for Twig’s null-safe
?.operator so templates can traverse objects and arrays without defensive plumbing:{{ user?.address?.city }}now renders the value when present and stays silent when any hop isnull.?.reuses the existingGetAttrExpressionoperator, so it should have otherwise the same precedence and behavior as..Usage cases can be found in the tests:
Twig/tests/ExpressionParserTest.php
Lines 304 to 343 in 7d98d45
Tests cover both the rendered output and the generated PHP.
Let me know if any tests are missing or if the implementation should be done any other way.