Home >>Laravel Tutorial >Laravel Blade Templates
Blade is provided in laravel which is the powerful template engine. It does not restrict the use of plain PHP code in views. Blade views are compiled into plain PHP code. The advantage of using blade template is that it can easily be expanded to other files by using master template. It uses .blade.php file extension and are stored in resources/views directory.
Following reasons are mentioned below why blade template engine is used:
Below shown example of control statements used in laravel. condition starts with special character '@'
<html> <body> @if(($id)==1) student id is equal to 1. @else student id is not equal to 1 @endif </body> </html>
In Laravel Blade template we can also use '@unless' for conditions.
<html> <body> @unless($id==1) student id is not equal to 1. @endunless </body> </html>
Another directive rather than the above two is '@hasSection' which looks for the content inside the section present or not.
<html> <body> <title> @hasSection('title') @yield('title') - PHP @else LARAVEL @endif </title> </body> </html>
Iterations provided in laravel blade template engine are similar to other iterative statements in PHP.
Syntax for loops in template engine are:
@for @endfor @foreach @endforeach @while @endwhile
Example shown below shows the for loop directive in blade template
value of x : @for($x=1;$x<11;$x++) {{$x}} @endfor
public function show() { return view('for'); }
Route::get('/for', 'BladeController@show');
Example shown below shows the foreach loop directive in blade template
@foreach($variables as $var) {{$var}} <br> @endforeach
public function show() { return view('foreach', ['variables'=>['var_one','var_two','var_three','var_four']]); }
Route::get('/foreach', 'BladeController@show');
Example shown below shows the while loop directive in blade template
@while($i<5) PHPTPOINT {{$i++}} <br> @endwhile
public function show($i) { return view('while', compact('i')); }
Route::get('/while/{i}', 'BladeController@show');