Fabien Potencier b1b2a0923f feature #4717 Add null-safe operator (felds)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Add null-safe operator

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 is `null`.

`?.` reuses the existing `GetAttrExpression` operator, so it should have otherwise the same precedence and behavior as `.`.

Usage cases can be found in the tests:
https://github.com/twigphp/Twig/blob/7d98d45f58df040f41a81f7f8d4cce6c42be781e/tests/ExpressionParserTest.php#L304-L343

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.

Commits
-------

263c04fd1f Add null-safe operator
2026-01-17 14:57:55 +01:00
2025-07-29 10:07:07 +02:00
2026-01-17 14:57:47 +01:00
2026-01-17 14:57:47 +01:00
2026-01-17 14:57:47 +01:00
2025-07-29 10:07:07 +02:00
2026-01-16 11:26:26 +01:00
2025-01-31 21:45:36 +01:00
2024-11-20 20:05:33 +01:00
2025-12-05 11:47:00 +01:00

Twig, the flexible, fast, and secure template language for PHP
==============================================================

Twig is a template language for PHP.

Twig uses a syntax similar to the Django and Jinja template languages which
inspired the Twig runtime environment.

Sponsors
--------

.. raw:: html

    <a href="https://docs.blackfire.io/introduction?utm_source=twig&utm_medium=github_readme&utm_campaign=logo">
        <img src="https://static.blackfire.io/assets/intemporals/logo/png/blackfire-io_secondary_horizontal_transparent.png?1" width="255px" alt="Blackfire.io">
    </a>

More Information
----------------

Read the `documentation`_ for more information.

.. _documentation: https://twig.symfony.com/documentation
Languages
PHP 99.9%