Allow the use of layout as a Component #1807
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
1️⃣ Is this something that is wanted/needed? Did you create an issue / discussion about it first?
I did not create an issue, but it's probably related with this one: #1748
2️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out.
No.
3️⃣ Does it include tests, if possible? (Not a deal-breaker, just a nice-to-have)
No.
4️⃣ Please include a thorough description of the improvement and reasons why it's useful.
When using layout as a Component, like it's done in Jetstream (as an example) for the AppLayout and GuestLayout, we are able to define some "complex data" we don't want to put in our blade file:
But since our blade file is strongly attached to our Component class, some errors appear when we are trying to render our Livewire components using its class directly:
As mentioned in a thread on the Livewire forum, I've succeed into resolving it using base Livewire methods by manually instantiate the AppLayout class and passing its data as parameters:
But manually add this code, referencing the default layout and passing its data manually is totally boring and we don't want that.
So I've dive into the Livewire code and searched why I needed to do that.
I finally understood that Livewire do not use x-blade component syntax, which is (I suppose) the source of the problem.
First I tried to search for a method to programmatically know if x-component was used by the layout file, but didn't find one (open to suggestion).
Then I figured out that I just needed to create a view file to render the layouts that are extending Component class
I've made some tests, using a x-blade specific layout to render my components and it worked perfectly well.
Since x-blade syntax is not natively supported by Laravel 7 which is supported by Livewire and assuming the fact that most of us don't use Blade Component to render layouts, I propose to just set a config boolean to change the default behavior to use this feature.
I've created the viewMacro method to change the default layout and pass data to it the same way we use
->layout()
or->extend()
. You can just use->xLayout('custom-layout', ['parameter' => 'custom data'])
Doing so, we can use basic feature for some layout, and xLayout to render specific Component extending layout.
I'm totally open to suggestion to upgrade my code, I think this feature in my implementation-style or another should be merged in near future.
5️⃣ Thanks @calebporzio for providing this good package! 🙌