-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
This PR adds support for the optional chaining operator (?.) to Twig. #4623
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?
Conversation
|
./vendor/bin/simple-phpunit tests/Extension/OptionalChainingTest.php Testing Twig\Tests\Extension\OptionalChainingTest Time: 00:00.036, Memory: 10.00 MB |
stof
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to run the bin/generate_operators_precedence.php script to regenerate the list of operators in the documentation to include your new operator.
| ) | ||
| ) { | ||
|
|
||
| return new MacroReferenceExpression(new TemplateVariable($expr->getAttribute('name'), $expr->getTemplateLine()), 'macro_'.$attribute->getAttribute('value'), $arguments, $expr->getTemplateLine()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to ignore the fact that this is an optional chain. Is it expected ?
|
|
||
| // all literals | ||
| new LiteralExpressionParser(), | ||
| new OptionalChainExpressionParser(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be registered near other get attribute operators, not in the section labelled as all literals
| public const REGEX_INLINE_COMMENT = '/#[^\n]*/A'; | ||
| public const PUNCTUATION = '()[]{}?:.,|'; | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be reverted
| } | ||
|
|
||
| parent::__construct($nodes, ['type' => $type, 'ignore_strict_check' => false, 'optimizable' => true], $lineno); | ||
| parent::__construct($nodes, ['type' => $type, 'ignore_strict_check' => false, 'optimizable' => true, 'is_optional_chain' => $isOptionalChain], $lineno); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need another attribute for the optional chain ? Couldn't it be a matter of the ignore_strict_check attribute ?
| $compiler->raw(', ') | ||
| ->repr($this->getAttribute('type')) | ||
| ->raw(', ')->repr($this->definedTest) | ||
| ->raw(', ')->repr($this->definedTest ?? false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change ?
| $compiler->raw(')'); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this empty line between methods should not be removed
|
|
||
| use Twig\Test\IntegrationTestCase; | ||
|
|
||
| class OptionalChainingTest extends IntegrationTestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no OptionalChaining extension. The operator is registered by CoreExtension. This should be tested by the existing integration tests by putting the fixtures in the existing folder.
|
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 |
|
I'm dying for optional chaining in 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 }}`