Home >>Laravel Tutorial >Laravel Blade Templates

Laravel Blade Templates

Blade Templates in Laravel

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.

Blade Template Usage

Following reasons are mentioned below why blade template engine is used:

  • Data Display :In core php we use to display data by using the variable name declared like <?php echo $var ?> or <?= $var ?> and in blade template we simply use {{ $var }} variable inside curly braces.
  • Ternary operator : Old php style of writing ternary operator is <?= isset($var) ? $var : ? set_value ?> which can be easily written in blade as {{ $var or 'set_value'}}

Control Statements in Blade Template

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> 

Use Loops in Blade Templates

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

Use @for loop in Blade

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');  
  1. Create a blade file in resources/views folder named as for.blade.php and add the following code:
  2. Make a controller naming BladeController.php and add the following code:
  3. Finally make Route for page in web.php
Output :
for loop in blade templates

Use @foreach loop in Blade

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');  
  1. Create a blade file in resources/views folder named as foreach.blade.php and add the following code:
  2. Make a controller naming BladeController.php and add the following code:
  3. Finally make Route for page in web.php
Output :
foreach loop in blade templates

Use @while loop in Blade

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');  
  1. Create a blade file in resources/views folder named as while.blade.php and add the following code:
  2. Make a controller naming BladeController.php and add the following code:
  3. Finally make Route for page in web.php
Output :
While loop in blade templates

No Sidebar ads