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

[Proposal] Blade @foreach/@empty #5009

Closed
Nirlah opened this issue Jul 10, 2014 · 8 comments
Closed

[Proposal] Blade @foreach/@empty #5009

Nirlah opened this issue Jul 10, 2014 · 8 comments

Comments

@Nirlah
Copy link
Contributor

Nirlah commented Jul 10, 2014

I thought it would be nice if there was an elegant way to handle empty states using Blade:

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@empty
    <p>No users yet.<a href="#">Create the first one...</a></p>
@endforeach

Instead of the need to use both if/else and foreach.

@Nirlah Nirlah changed the title [Proposal] Blade @foreach @empty [Proposal] Blade @foreach/@empty Jul 10, 2014
@bsteephenson
Copy link

An @endempty tag would also make the syntax look cleaner

@barryvdh
Copy link
Contributor

Or similar to Twig, foreach.. else.. endforeach
This was actually in Laravel 3, not sure why it was removed, probably because it wasn't so easy to implement/maintain. See #49

@franzliedke
Copy link
Contributor

@GrahamCampbell The Blade parser does no syntax validation, does it? If you write invalid syntax, you will get some PHP parsing errors.

@franzliedke
Copy link
Contributor

Oh, never mind. Didn't see that it was called @foreach here. It would need to be something different, like @forelse.

@JoostK
Copy link
Contributor

JoostK commented Jul 11, 2014

The problem with L3's @forelse was that it may execute a function call twice, i.e. first in the if guard and then in the foreach statement.

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, foreach(...) expands directly into a foreach but with the extra state variable. Note that this variable will then be there even for statement which don't have the @forelse construct, but that isn't that bad (and could be solved by using a different keyword but I'd rather see it like this).

The other expansions are pretty straightforward from the example.

@taylorotwell
Copy link
Member

@JoostK if you want to give it a shot, make a pull request. It looks pretty decent to me. You up for it?

@JoostK
Copy link
Contributor

JoostK commented Jul 11, 2014

I can do that. I guess I will PR this to 4.2.

@KennedyTedesco
Copy link
Contributor

@JoostK Yes, this is not a breaking change. :)

@JoostK JoostK mentioned this issue Jul 11, 2014
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

No branches or pull requests

7 participants