Blade templating engine in laravel 5

Writing html code with standard php where you write too many php and html tags. This type of code may be complicated, messy and less readable. That's why lot of developers using blade templates in laravel.

Blade is a simple and powerful templating engine. Every blade template must use .blade.php extention.

If you getting Error: Class 'Illuminate\Html\HtmlServiceProvider' not found. Then add Form/html package in laravel.

If you don't know how to add new package them you can find solution, Check Add Form/html package in laravel5.

Look at this simple example :-

App/Http/routes.php

<?php
Route::get('user', function() {
    $users = App\User::all();
    return view('userDir.users', compact('users'));
});

 

resources/view/userDir/users.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>User list page</title>
</head>
<body>

<h1>Our User List</h1>

// with standard php 
<?php
foreach( $users as $user ){
	echo '<li>'.  $user->name .'</li>';
}
?>

// with Blade templating engine
@foreach($users as $user)
    <li>{{ $user->name }}</li>
@endforeach

</body>
</html>

Look at above code, Trying to show difference between standard php code and blade engine code, Its very clear blade syntax is too much clean.

Just Keep in mind :

{{ }} :: <?php= ?> and never put semicolon(;) between those braces.

@ :: <?php ?>

{!! !!} :: For escape html characters  

<?php
// {!! !!} for escape html characters
    <li>{!! link_to( "user/{$user->name}", $user->name ) !!}</li>

// Another example 
{!! '<script>alert('Hello, World!);</script>' !!}

 

If statements with standard php syntax:

<?php
if(something){
	<p>something is true</p>
}else{
	<p>something is false</p>
}
?>

 

If statements with blade syntax:

You may find much cleaner than above code here we stripped out <?php ?> and several curly braces.

@if(something)
      <p>something is true</p>
@else
      <p>something is false</p>
@endif

 

Unless syntax with blade:

@unless (Auth::check())
    You are not signed in.
@endunless

 

Looping

For Loop:

@for( $i=1; $i<100; $i++ )
	<p>Number {{ $i }} is less than 100</p>
@endfor

While Loop:

@while(true)
@endwhile

 

Something to say? Tell us in comment section.