Blade is Laravel's built-in templating engine. It allows you to display dynamic data, write conditions and loops, reuse templates, and create clean HTML pages.
Blade is Laravel's template engine for creating
dynamic HTML pages. Blade templates usually have
the .blade.php extension.
Example file:
resources/views/home.blade.php
Example Blade template:
<h1>Welcome to Laravel</h1>
<p>Learn PHP with Soopro Pathshala.</p>
Blade templates can contain HTML, Blade directives, and PHP expressions. Laravel processes the template and returns the resulting HTML to the browser.
Blade uses double curly braces to display a variable.
<h1>{{ $name }}</h1>
<p>Course: {{ $course }}</p>
Suppose your controller passes these variables:
$name = 'Rahul';
$course = 'Laravel';
return view('student', compact('name', 'course'));
The output will be:
Course: Laravel
Blade's {{ }} syntax escapes output
by default, helping protect against HTML injection.
Use @if, @elseif,
@else, and @endif
to display content based on conditions.
@if ($marks >= 40)
<p>Result: Pass</p>
@else
<p>Result: Fail</p>
@endif
Example with multiple conditions:
@if ($marks >= 80)
<p>Grade: A</p>
@elseif ($marks >= 60)
<p>Grade: B</p>
@elseif ($marks >= 40)
<p>Grade: C</p>
@else
<p>Grade: Fail</p>
@endif
Blade evaluates the conditions and renders the matching section.
Use @foreach to display items from an
array or collection.
Blade example:
<h2>Available Courses</h2>
<ul>
@foreach ($courses as $course)
<li>{{ $course }}</li>
@endforeach
</ul>
Example data passed from a controller:
$courses = [
'PHP',
'Laravel',
'JavaScript',
'React'
];
return view('courses', compact('courses'));
Output:
The @forelse directive loops through
items and provides an alternative when the list
contains no items.
<h2>Student List</h2>
<ul>
@forelse ($students as $student)
<li>{{ $student }}</li>
@empty
<li>No students found.</li>
@endforelse
</ul>
If the collection is empty, Laravel displays
the message inside @empty.
You can also use @isset to check
whether a variable exists and is not null.
@isset($name)
<p>Student Name: {{ $name }}</p>
@endisset
Blade supports comments that are removed when the template is rendered.
{{-- This is a Blade comment --}}
<h1>Welcome to Laravel</h1>
Unlike HTML comments, Blade comments are not included in the rendered HTML output.
Blade also supports the @php directive
for small amounts of PHP code.
@php
$name = 'Amit';
$course = 'Laravel';
@endphp
<p>Name: {{ $name }}</p>
<p>Course: {{ $course }}</p>
For larger operations or business logic, prefer controllers or dedicated classes rather than putting complex PHP code in a view.
Blade's double curly braces escape HTML by default. This is the recommended choice for most dynamic data.
{{ $message }}
If $message contains HTML, the tags
will normally be displayed as text rather than
rendered as HTML.
Blade also provides unescaped output syntax:
{!! $html !!}
Security warning: Use
{!! !!} only when the HTML is trusted
or has been properly sanitized. Rendering
untrusted user input this way can introduce
cross-site scripting (XSS) vulnerabilities.
The @include directive lets you reuse
a Blade view inside another view.
Create:
resources/views/header.blade.php
Inside header.blade.php:
<header>
<h2>Soopro Pathshala</h2>
<hr>
</header>
Use it inside home.blade.php:
@include('header')
<h1>Welcome to the Home Page</h1>
<p>Learn Laravel step by step.</p>
Laravel renders the included view at that location. You can also pass data to an included view.
@include('header', ['title' => 'Laravel Tutorial'])
Inside the included view, the
$title variable is available.
Complete these exercises to practise Blade templates.
@if to show Pass or Fail based on marks.@foreach.@forelse to display an empty-list message.@php to define a simple variable.@include.{{ }} with raw output {!! !!}.Remember to keep complex business logic in controllers and use Blade primarily for presentation.
1. What is the file extension of a Blade template?
2. Which directive loops through an array?
3. Which syntax escapes output by default?