Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only apply filter if not null #4389

Open
tacman opened this issue Oct 10, 2024 · 3 comments
Open

Only apply filter if not null #4389

tacman opened this issue Oct 10, 2024 · 3 comments

Comments

@tacman
Copy link

tacman commented Oct 10, 2024

@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 #3951 (comment)

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') }} 
@smnandre
Copy link
Contributor

The second part of the ternary is optional. The following will work the same

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

@stof
Copy link
Member

stof commented Oct 11, 2024

For a single filter, this works. For a longer expression, it is not the same (as it would force to apply filter twice, in the ternary condition and the ternary result expression

Also, omitting the else part of a ternary produces an empty string, not null

@smnandre
Copy link
Contributor

(just a precision on this case, not an opinion on the RFC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants