Skip to content

Template loop

Jorge Castro edited this page Apr 22, 2020 · 2 revisions

loop

@for($variable;$condition;$increment) / @endfor

Generates a loop until the condition is meet and the variable is incremented for each loop

Tag Note Example
$variable is a variable that should be initialized. $i=0
$condition is the condition that must be true, otherwise the cycle will end. $i<10
$increment is how the variable is incremented in each loop. $i++

Example:

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}<br>
@endfor

Returns:

The current value is 0
The current value is 1
The current value is 2
The current value is 3
The current value is 4
The current value is 5
The current value is 6
The current value is 7
The current value is 8
The current value is 9

@foreach($array as $alias) / @endforeach

Generates a loop for each values of the variable.

Tag Note Example
$array Is an array with values. $countries
$alias is a new variable that it stores each interaction of the cycle. $country

Example: ($users is an array of objects)

@foreach($users as $user)
    This is user {{ $user->id }}
@endforeach

Returns:

This is user 1
This is user 2

@forelse($array as $alias) / @empty / @endforelse

Its the same as foreach but jumps to the @empty tag if the array is null or empty

Tag Note Example
$array Is an array with values. $countries
$alias is a new variable that it stores each interaction of the cycle. $country

Example: ($users is an array of objects)

@forelse($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

Returns:

John Doe
Anna Smith

@while($condition) / @endwhile

Loops until the condition is not meet.

Tag Note Example
$condition The cycle loops until the condition is false. $counter<10

Example: ($users is an array of objects)

@set($whilecounter=0)
@while($whilecounter<3)
    @set($whilecounter)
    I'm looping forever.<br>
@endwhile

Returns:

I'm looping forever.
I'm looping forever.
I'm looping forever.

@splitforeach($nElem,$textbetween,$textend="") inside @foreach

This functions show a text inside a @foreach cycle every "n" of elements. This function could be used when you want to add columns to a list of elements.
NOTE: The $textbetween is not displayed if its the last element of the last. With the last element, it shows the variable $textend

Tag Note Example
$nElem Number of elements 2, for every 2 element the text is displayed
$textbetween Text to show </tr><tr>
$textend Text to show </tr>

Example: ($users is an array of objects)

<table border="1">
<tr>
@foreach($drinks7 as $drink)
    <td>{{$drink}}</td>
    @splitforeach(2,'</tr><tr>','</tr>')
    @endforeach
</table>

Returns a table with 2 columns.

Example: ($users is an array of objects)

<ul>
@foreach($drinks7 as $drink)
    <li>{{$drink}}</li>
    @splitforeach('c3','</ul></ul>','')
    @endforeach
</ul>

Returns 3 unsorted lists.

@continue / @break

Continue jump to the next iteration of a cycle. @break jump out of a cycle.

Tag Note Example

Example: ($users is an array of objects)

@foreach($users as $user)
    @if($user->type == 1) // ignores the first user John Smith
    @continue
    @endif
    <li>{{ $user->type }} - {{ $user->name }}</li>

    @if($user->number == 5) // ends the cycle.
        @break
    @endif
@endforeach

Returns:

2 - Anna Smith
Clone this wiki locally