[5.7] Remove Blade defaults#23532
[5.7] Remove Blade defaults#23532taylorotwell merged 1 commit intolaravel:masterfrom browner12:blade-or
Conversation
current Blade functionality allows the user to define a default value if the first value is not set.
`{{ $name or 'Andrew' }}`
This feature was added in an early version of Laravel to be a shorthand for
`isset($name) ? $name : 'Andrew'`
PHP7 has introduced the null coalesce operator (`??`) which performs this exact functionality.
`$name ?? 'Andrew'`
While the 'or' feature was good when it was needed, it could produce unexpected results when the characters 'or' appeared anywhere in the Blade expression. This was solved with extensive regex, but was still somewhat error prone.
Now that Laravel requires PHP 7.1, we can utilize the null coalesce operator and remove the 'or' feature. User upgrade process will be a find and replace, and the number of characters will be identical.
```
{{ $name or 'Andrew' }}
{{ $name ?? 'Andrew' }}
```
|
I went back and forth on removing some of the additional tests. They are testing to make sure that 'or' inside of a string or as a variable name are not compiled into the ternary. I'm not sure they are really testing anything anymore, and it might be confusing for someone coming into the file to see them in the future. If people agree, I'll remove them as well. |
|
Achievement unlocked. Get a PR merged that only removes code! |
|
But like... why break people's templates unnecessarily? |
|
because it causes a lot of issues, and PHP now offers a native feature that performs this more reliably. |
|
Ok. But why removing? Let it be somewhere alternative way to achieve the newer functionality. Definitely this will break many templates and hard to find and convert for bigger websites. |
|
It will only break for people that don't follow the upgrade guide when migrating from 5.6 to 5.7. |
|
Can we not keep previous logic in addition to the new logic and mark it as deprecated in the spirit of laravel/ideas#383 ? |
|
@MadMikeyB because 5.7 is a next major release and its not that hard to search for This should significantly increase performance of big projects because it removes regular expressions and replaces. |
|
Understood @a-komarev however this will introduce BC breaks which can be avoided by deprecating first before entirely removing. |
|
It has been deprecated since PHP 7.0 added support for |
current Blade functionality allows the user to define a default value if the first value is not set.
{{ $name or 'Andrew' }}This feature was added in an early version of Laravel to be a shorthand for
isset($name) ? $name : 'Andrew'PHP7 has introduced the null coalesce operator (
??) which performs this exact functionality.$name ?? 'Andrew'While the 'or' feature was good when it was needed, it could produce unexpected results when the characters 'or' appeared anywhere in the Blade expression. This was solved with extensive regex, but was still somewhat error prone.
Now that Laravel requires PHP 7.1, we can utilize the null coalesce operator and remove the 'or' feature. User upgrade process will be a find and replace, and the number of characters will be identical.