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

[8.x] Adds class handling for Blade echo statements #37477

Closed
wants to merge 4 commits into from
Closed

[8.x] Adds class handling for Blade echo statements #37477

wants to merge 4 commits into from

Conversation

lukeraymonddowning
Copy link
Contributor

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 a Carbon 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

// AppServiceProvider
Blade::handle(Money::class, fn($object) => $object->formatTo('en_GB'));
<dl>
    <dt>Total</dt>
    <dd>{{ $total }}</dd> <!-- This is a money object, but will be outputted as an en_GB formatted string -->
</dl>

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!

@driesvints
Copy link
Member

Heya. Seems like this PR breaks the test suite. Feel free to attempt again with passing tests.

@driesvints driesvints closed this May 25, 2021
@lukeraymonddowning
Copy link
Contributor Author

@driesvints on it now 👍 Its just because it wraps existing output. Will reopen once fixed.

@lukeraymonddowning
Copy link
Contributor Author

@driesvints any chance of reopening this? I've fixed the test suite 👍

@lukeraymonddowning
Copy link
Contributor Author

Nevermind, opened a new one #37478

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

Successfully merging this pull request may close these issues.

3 participants