#4788·Twig

Add support for a null-safe filter operator ?|

Author: ehymelCreated Mar 21, 2026Updated Mar 21, 2026

My twig templates are full of code where I have a parameter, usually a date, that I need to display if it exists:

twig
{{ site.startDate ? site.startDate|date('m/d/Y') }}

It would be nice if this could be shortened to something like this:

twig
{{ site.startDate ?| date('m/d/Y') }}

I know that I could just write a a custom date filter that handles null input, along the lines of below, but this seems a bit much:

twig
#[AsTwigFilter(name: 'safe_date', needsEnvironment: true)]
    public function safeDate(Environment $env, $date, ?string $format = null, $timezone = null): string
    {
        if (empty($date)) {
            return '';
        }
        return $env->getExtension(CoreExtension::class)->formatDate($date, $format, $timezone);
    }