It would be great to have functionality similar to what jinja2 provides in blade loops:
| Variable | Description |
| --- | --- |
| loop.index | The current iteration of the loop. (1 indexed) |
| loop.index0 | The current iteration of the loop. (0 indexed) |
| loop.revindex | The number of iterations from the end of the loop (1 indexed) |
| loop.revindex0 | The number of iterations from the end of the loop (0 indexed) |
| loop.first | True if first iteration. |
| loop.last | True if last iteration. |
| loop.length | The number of items in the sequence. |
| loop.cycle | A helper function to cycle between a list of sequences. See the explanation below. |
| loop.depth | Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 1 |
| loop.depth0 | Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 0 |
More information on this is in the jinja2 docs: http://jinja.pocoo.org/docs/dev/templates/#for
Hmm. Interesting. Ping @taylorotwell.
Twig can do this too: http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable
Edit: As do Volt and Smarty (https://docs.phalconphp.com/en/latest/reference/volt.html#loop-context, http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.last)
This would allow for so much cleaner templating in blade. The cruft currently needed for an index or checking whether it is the last iteration is really quite bad atm.
<?php
$itemcount = $iterator->count();
$index = 0;
?>
@foreach($iterator as $key -> $item)
<?php $index++; ?>
<li class="item {{ (($index == $itemcount) ? "lastitem" : "") }}">$item</li>
@endforeach
could be written as
@foreach($iterator as $key -> $item)
<li class="item {{ (($loop->last) ? "lastitem" : "") }}">$item</li>
@endforeach
@Yannik You can already do it using this http://robin.radic.nl/blade-extensions/
Thanks for the link to this blade extensions bundle.
However, the loop control stuff would be really great to see as a feature natively integrated into blade - just as with Twig, Volt, Smarty, Jinja2. It is such a commonly needed feature.
I've always wanted this as well. Another thing I've wanted is some kind of 'safe' variable definition in foreach loops. For instance:
<?php $user = Auth::user(); ?>
@foreach (User::all() as $user)
{{ $user->name }}
@endforeach
Welcome, {{ $user->name }} // I want it to output the original one defined Auth::user()
I'm pretty sure twig / jinja and others have 'safe' foreach loops which creates a new context for the variable and won't overwrite the existing one, so it's safe to use after the loop.
Sorry, we don't process requests/proposals here. Please see https://github.com/laravel/internals/issues.
@GrahamCampbell I don't know who the account "laravel-internals" belongs to.
Did @taylorotwell have a look at this?
The account belongs to the laravel core team.
Taylor tends not to read the issues posted on this repo, so our new internals repo has been created to allow people to discuss proposals. Our issues on the framework repo a strictly for bugs only.
I've submitted a PR that extends Blade's looping functionality as proposed in this issue https://github.com/laravel/framework/pull/12867
Most helpful comment
I've always wanted this as well. Another thing I've wanted is some kind of 'safe' variable definition in foreach loops. For instance:
I'm pretty sure twig / jinja and others have 'safe' foreach loops which creates a new context for the variable and won't overwrite the existing one, so it's safe to use after the loop.