-
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
[Proposal] Blade @foreach/@empty #5009
Comments
An @endempty tag would also make the syntax look cleaner |
Or similar to Twig, foreach.. else.. endforeach |
@GrahamCampbell The Blade parser does no syntax validation, does it? If you write invalid syntax, you will get some PHP parsing errors. |
Oh, never mind. Didn't see that it was called |
The problem with L3's I think this can easily be avoided by the following approach: @foreach($this->getUsers() as $user)
<li>{{ $user->name }}</li>
@forelse
<p>No users</p>
@endforelse <?php $__empty = true; foreach($this->getUsers() as $user): $__empty = false; ?>
<li><?php echo $user->name; ?></li>
<?php endforeach; ?>
<?php if($__empty): ?>
<p>No users</p>
<?php endif; ?> So, The other expansions are pretty straightforward from the example. |
@JoostK if you want to give it a shot, make a pull request. It looks pretty decent to me. You up for it? |
I can do that. I guess I will PR this to 4.2. |
@JoostK Yes, this is not a breaking change. :) |
I thought it would be nice if there was an elegant way to handle empty states using Blade:
Instead of the need to use both
if/else
andforeach
.The text was updated successfully, but these errors were encountered: