<html>
<head>
<title>Example of Loop</title>
<!-- Google Fonts -->
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
<!-- CSS Reset -->
<link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css">
<!-- Milligram CSS minified -->
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
</head>
<body>
<h1>Example of Loops</h1>
<h2>@@for</h2>
<pre>
@@for ($i = 0; $i < 10; $i++)
The current value is @{{ $i }}
@@endfor
</pre>
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}<br>
@endfor
<br><h2>@@foreach</h2>
<pre>
@@foreach ($users as $user)
This is user @{{ $user->id }}
@@endforeach
</pre>
@foreach ($users as $user)
This is user {{ $user->id }}<br>
@endforeach
<br><h2>@@splitforeach</h2>
<em>Split a foreach cycle by showing a text each "x" number of elements.
The text is not displayed if its the last element of the list</em><br>
<pre>
</pre>
<pre>
<table border="1">
<tr>
@@foreach ($drinks7 as $drink)
<td>@{{$drink}}</td>
@@splitforeach(3,'</tr><tr>','</tr><tr><td colspan=3>end of the table</td></tr>')
@@endforeach
</table>
</pre>
<table border="1">
<tr>
@foreach ($drinks7 as $drink)
<td>{{$drink}}</td>
@splitforeach(3,'</tr><tr>','</tr><tr><td colspan=3>end of the table</td></tr>')
@endforeach
</table>
with even number of elements: (with using the end parameter) <br>
<pre>
<table border="1">
<tr>
@@foreach ($drinks8 as $drink)
<td>@{{$drink}}</td>
@@splitforeach(2,'</tr><tr>','</tr><tr><td colspan=2>end of the table</td></tr>')
@@endforeach
</table>
</pre>
<table border="1">
<tr>
@foreach ($drinks8 as $drink)
<td>{{$drink}}</td>
@splitforeach(2,'</tr><tr>','</tr><tr><td colspan=2>end of the table</td></tr>')
@endforeach
</table>
A more complex exercise, using variable <br>
<pre>
<table border="1">
@@set($even=0)
<tr><td style="background-color:lightblue">@{{$even/2+1}}</td>
@@foreach ($drinks8 as $drink)
@@set($even)
@@if($even % 4 ==0 || ($even+1) % 4 ==0)
@@set($stylerow="#ffffff")
@@else
@@set($stylerow="#909090")
@@endif
<td style="background-color:@{{$stylerow}}">@{{$drink}}</td>
@@splitforeach(2,'</tr><tr><td style="background-color:lightblue">'.($even/2+1).'</td>','</tr><tr><td colspan=3>end of the table</td></tr>')
@@endforeach
</table>
</pre>
<table border="1">
@set($even=0)
<tr><td style="background-color:lightblue">{{$even/2+1}}</td>
@foreach ($drinks8 as $drink)
@set($even)
@if($even % 4 ==0 || ($even+1) % 4 ==0)
@set($stylerow="#ffffff")
@else
@set($stylerow="#909090")
@endif
<td style="background-color:{{$stylerow}}">{{$drink}}</td>
@splitforeach(2,'</tr><tr><td style="background-color:lightblue">'.($even/2+1).'</td>','</tr><tr><td colspan=3>end of the table</td></tr>')
@endforeach
</table>
<br><h2>@@forelse</h2>
<pre>
@@forelse ($users as $user)
<li>@{{ $user->name }}</li>
@@empty
<p>No users</p>
@@endforelse
</pre>
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
<br><h2>@@while</h2>
<pre>
@@set($whilecounter=0)
@@while ($whilecounter<3)
@@set($whilecounter)
I'm looping forever.<br>
@@endwhile
</pre>
@set($whilecounter=0)
@while ($whilecounter<3)
@set($whilecounter)
I'm looping forever.<br>
@endwhile
<br><h2>@@continue/break (foreach)</h2>
<pre>
@@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
</pre>
@foreach ($users as $user)
@if($user->type == 1)
@continue
@endif
{{ $user->type }} - {{ $user->name }}<br>
@if($user->number == 5)
@break
@endif
@endforeach
<pre>
@@foreach ($users as $user)
@@continue($user->type == 1)
@{{ $user->type }} - @{{ $user->name }}</br>
@@break($user->number == 5)
@@endforeach
</pre>
@foreach ($users as $user)
@continue($user->type == 1)
{{ $user->type }} -{{ $user->name }}</br>
@break($user->number == 5)
@endforeach
<br><h2>@@each</h2>
<pre>
@@each('Test.InnerView.name', $records, 'job')
</pre>
where Test.InnerView.name contains
<pre>
<hr>
@{{$job}}
<hr>
</pre>
@each('Test.InnerView.name', $records, 'job')
</body>
</html>
|