#9 Blade Templates Laravel 5.6

Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.

For loop

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

Foreach loop

foreach( $array as $data ) {
  echo 'test';
}

@foreach ( $array as $data )
  {{$data}}
  @endforeach
$count = 1;
foreach( $users as $user ) {
  if ( 1 === $count  ) {
    echo 'This is the first iteration.';
  }
  
  if ( $count === count( $users ) ) {
    echo 'This is the last iteration';
  }
  
  $count++
}


@foreach ($users as $user)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>This is user {{ $user->id }}</p>
@endforeach
@foreach ($users as $user)
    @foreach ($user->posts as $post)
        @if ($loop->parent->first)
            This is first iteration of the parent loop.
        @endif
    @endforeach
@endforeach

While loop

while( true ) {
  <p>I'm looping forever.</p>
}

@while (true)
    <p>I'm looping forever.</p>
@endwhile

Add your review



Leave a Reply