-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[8.x] Adds class handling for Blade echo statements #37478
[8.x] Adds class handling for Blade echo statements #37478
Conversation
This is a fixed version of #37477 which had broken unit tests. It also is faster than previously because it will completely skip the JIT check if no handlers have been specified. |
Does putting line breaks in the compiled echo syntax cause any unwanted side effects in regards to rendered elements? For example: Foo{{ $bar }}baz And: Foo: {{ $bar }} Is the proper spacing always maintained when rendered? |
Will check this tomorrow and add unit tests to prove |
@taylorotwell from testing in a real application, the whitespaces made no difference, but it doesn't really make much sense to keep the whitespaces there anyway so I've removed them. Also added a test in similar vain to those in the |
* | ||
* @return void | ||
*/ | ||
public function handle(string $className, callable $handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if this should be addEchoHandler
. handle
is so generic and might be confused with a job's handle
method. The former is a registration of a resolver while the later is an actual resolver.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I've renamed.
Renamed method to Blade::stringable(fn (Money $money) => $money->formatTo('en_GB')); Thoughts? |
Love it @taylorotwell. Stringable fits in perfectly with the interface, so makes complete sense and the ability to type hint instead is pure fire 😎 |
What is it?
When working in Blade, I'll often want to always output a class in the same manner. I'll use Brick/Money as the example here, but this can apply to pretty much any class you can think of.
Brick/Money is a final class, so I can't add a
Stringable
interface to it. Whenever I output{{ $moneyObject }}
, I would want it to format as£100.00
, for example. The same could be said for aCarbon
instance.This PR adds a new
Blade::handle()
method that can be placed in the boot method of a Service Provider and allows the user to add intercepting closures for any class. The returned value will be outputted in Blade.Think of this as the Exception Handler, but for Blade.
Usage
How does it work?
This works by wrapping a small piece of logic around php output at the parser level, which means that the object is evaluated just in time and can thus be treated as a standard object throughout the entire application; it is only transformed by the PHP script at the time of output.
Feel free to throw any questions back at me :-)
Thanks for all the hard work guys!