/twig-nullsafe

Adds a nullsafe operator ('?.') to twig.

Primary LanguagePHPMIT LicenseMIT

Twig Nullsafe Operator

The nullsafe (also called "optional chaining") operator exists in PHP 8 and many other programming language, but not twig. The aim of this package was to bring it's convenience to twig as well. See discussion.

License Build Status

Phpstan Level Easy Coding Standard

Note By building and testing this POC, I came to understand the existing behavior and abilities of Twig better, and decided that this functionality is not actually that valuable relative to what's possible already. Hence, I will not be developing this any more unless convinced otherwise. Read my reasoning here.

There are also probably a lot of edge cases that this doesn't handle quite right, so while it is not recommended for production, perhaps it will inspire a more robust implementation. But perhaps not, as to do this right, I think it actually would require modifications to core, as opposed to an extension. ('.' is parsed by the core subscript expression parser, as opposed to an operator as is used here, and it would make the most sense to handle '?.' in the same place).

Usage

Load this extension as appropriate for your framework. Then

{# my_template.twig #}
Some content
{{ a?.b }} {# outputs a.b if a is defined, nothing otherwise #}
More content