This PR adds support for the optional chaining operator (?.) to Twig.
This PR adds support for the optional chaining operator (?.) to Twig.
The optional chaining operator (?.) enables developers to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.
Example usage:
{{ foo?.bar?.baz }}
This is equivalent to:
{{ foo.bar.baz is defined ? foo.bar.baz }}`
./vendor/bin/simple-phpunit tests/Extension/OptionalChainingTest.php
Testing Twig\Tests\Extension\OptionalChainingTest ......S 7 / 7 (100%)
Time: 00:00.036, Memory: 10.00 MB
So we doing this or nah?
@fabpot What do you think of this idea? Today I had again a situation where this would have helped tremendously by doing something like {% if app.user.account.id == some.other.id %} and account being null.
I'm dying for optional chaining in twig 🥵