In Laravel framework blade is a powerful PHP templating engine system. There are many PHP template engine like Twig (for Symfony Framework), Smarty (for CodeIgniter Framework, also Twig or more can be use), Yii View (for Yii Framework). Today we will describe what is Laravel Blade and all features.
What is Blade template in Laravel
Blade is a default PHP template engine. With Laravel blade we can easily control the view in the Laravel Application.
Pass Variable in Laravel Blade from Controller
return view('create',['name' => 'Sujon Kumar Dey']); //when only view
return view('edit',compact('student'),['name' => 'Sujon Kumar Dey']); //when compact
print on the blade {{ $name }}
Feature of Blade Template
- Blade template prevent XSS Attack
- CSRF protection form.
- In blade template you can write PHP code.
- Built in validation on form.
- Easily variable/data transfer to blade templates
X-CSRF-TOKEN
<meta name="csrf-token" content="{{ csrf_token() }}">
@php in Blade Template
@php
//php code here
@endphp
If Else in Blade Template
@if (condition)
First one true
@elseif(condition)
Second one true
@else
No one true
@endif
@unless in Blade Template
@unless (condition) //if the condition false then it's will execute the block
Condition is not true
@endunless
@isset in Blade Template
@isset($variable)
// if variable not null then this blocks execute
@endisset
@empty($variable) in Blade Template
@empty($variable)
//if variable is empty then this block execute
@endempty
@auth in Blade Template
@auth
//if user is login this block will execute
@endauth
@guest in Blade Template
@guest
// if user is not login, mean if guest then this block will execute
@endguest
@foreach in Blade Template
@foreach ($variables as $variable)
{{ $variable->data }}
@endforeach
@forelse in Blade Template
@forelse ($variables as $variable)
{{ $variable->data }}
@empty
No data
@endforelse
@forelse in Blade Template
<span @class([
'my_static_class',
'my_class_based_on_logic' => $variable,
'my_class_based_on_logic_2' => $variable2,
])></span>
@class([''])
//if variable is true then class 'my_class_based_on_logic' will be work, if variable is false then class will not print
Comment in Blade
{{-- my comment --}}
Reference
https://laravel.com/docs/10.x/blade#the-at-verbatim-directive
https://laravel.com/docs/10.x/csrf