-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[10.x] Allow component slots to be empty, even if they have an html comment or line break #49935
Conversation
Can we just make an extra method that is like |
When I looked at this pull request I wondered if this was really useful, because there may be cases where we want to pass the comment to the slot. Now that Taylor commented on the possible breaking change, perhaps an auxiliary method would be interesting, as he said, or perhaps even a method that activates this cleaning behavior. If Taylor's idea goes forward, I suggest that the helper method be |
Drafting until updated. Please mark as ready for review if you want me to take another look. |
Hej @taylorotwell, thanks for your review and input. Are you ok with something like I modified this PR and provided a public function sanitize(null|string|callable $callable = null)
{
if (is_string($callable) && ! function_exists($callable)) {
throw new \InvalidArgumentException("Callable does not exist.");
}
$this->sanitizerResolver =
$callable ??
fn ($input) => trim(preg_replace("/<!--([\s\S]*?)-->/", '', $input)); // replace everything between <!-- and --> with empty string
return $this;
} Picking up my example above, we now have different options: The well-known behavior: @if($slot->isEmpty())
...
@endif The HTML sanitizer removes all @if($slot->sanitize()->isEmpty())
...
@endif If, for whatever reason, someone loves to have it a bit more flexible, the @if($slot->sanitize('trim')->isEmpty())
...
@endif This approach is a bit more flexible than a If you think this is still a problem, I'm open to creating a No matter which solution wins, I think it will make things easier for laravel developers. |
Most likely he will prefer a helper method such as |
I created another PR #49966, an alternate implementation of the same problem. It is a single-function solution Please decide which one fits better in the framework. |
This PR addresses and solves an issue with Livewire V3 morph markers.
Since Livewire is injecting
morph markers
, we cannot safely ask if a slot is empty or not. The same happens if we add a linebreak or nonprintable characters like whitespaces or tabs. The slot is not empty and should be rendered.From a technical point of view, the slot is not empty, the blade compiler must render this.
But from a 'what should happen in the template' point of view, the slot might be empty, and the HTML should contain something that occurs in the other condition.
here is an example. Given is a component
table.blade.php
In the view, we use this
<x-table />
and do a loop. The@foreach()
is an empty result set; I expect that we seeNo Results.
another blade file
The slot content starts with
<!--
in line 2 and ends with-->
in line 6. If the loop is empty, our slot content is<!-- start loop --><!-- end loop -->
. Should we render it, or is the slot empty?The Filament package has a is_slot_empty helper, which addresses this issue.
Shouldn't we bring this into the Framework to make slots a bit more intelligent?