-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add support for a null-safe filter operator ?| #4788
Copy link
Copy link
Open
Description
My twig templates are full of code where I have a parameter, usually a date, that I need to display if it exists:
{{ site.startDate ? site.startDate|date('m/d/Y') }}It would be nice if this could be shortened to something like this:
{{ 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:
#[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);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels