#4389·Twig

Only apply filter if not null

Author: tacmanCreated Oct 10, 2024Updated Dec 11, 2025
LabelsInvestigate before 4.x

@fprochazka suggested a BC change so that the date filter returns blank instead of the current date.

Currently you have to do {{ user.birthday ? user.birthday|date('Y-m-d') : '' }} to avoid this.

Originally posted by @PrOF-kk in https://github.com/twigphp/Twig/issues/3951#issuecomment-2404415672

This got me thinking that a more generic solution would be to not apply filters to nulls, with a syntax similar to PHP's ?-> operator, like this;

 {{ user.birthday?|date('Y-m-d') }}