What is Blade in Laravel?
Blade is the simple, yet powerful templating engine included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP in your templates. All Blade templates are compiled into plain PHP code and cached until modified, meaning Blade adds essentially zero overhead to your application. Blade template files use the .blade.php extension and are typically stored in resources/views.
Without Blade vs With Blade
Without Blade: You would need to mix PHP and HTML manually, which can get messy and hard to maintain:
<?php if ($user_logged_in): ?>
<h1>Hello, <?php echo $user_name; ?>!</h1>
<?php else: ?>
<p>Please log in.</p>
<?php endif; ?>With Blade: The same template is cleaner, readable, and safer:
@if ($user_logged_in)
<h1>Hello, {{ $user_name }}!</h1>
@else
<p>Please log in.</p>
@endifReturning Blade Views
Blade views can be returned from routes or controllers using the view helper. Data may be passed to the view via the helper’s second argument:
Route::get('/', function () {
return view('greeting', ['name' => 'Finn']);
});Displaying Data
Variables passed to Blade views are displayed using double curly braces, which automatically escape output to prevent XSS:
Hello, {{ $name }}.You may also output the result of any PHP function:
The current UNIX timestamp is {{ time() }}.To display unescaped content (be cautious), use:
Hello, {!! $name !!}.Escaping Blade Syntax for JS Frameworks
If your frontend uses curly braces (e.g., Vue or React), you can escape Blade parsing with @:
Hello, @{{ name }}.Blade Directives: Conditional Statements
Blade provides elegant directives for PHP control structures:
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
@unless (Auth::check())
You are not signed in.
@endunlessBlade Loops
Loops work similarly to PHP but with Blade directives:
@foreach ($users as $user)
<p>User: {{ $user->name }}</p>
@endforeach
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelseThe $loop variable gives useful info inside loops:
$loop->index– Current iteration index (0-based)$loop->iteration– Current iteration (1-based)$loop->first/$loop->last– Checks first/last iteration$loop->parent– Access parent loop in nested loops
Template Inheritance
Create master layouts and extend them across multiple views:
// layouts/app.blade.php
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
// home.blade.php
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<h1>Welcome to Laravel</h1>
@endsectionBlade Components & Slots
Components allow reusable UI blocks:
// components/button.blade.php
<button class="btn btn-primary">{{ $slot }}</button>
// Usage
<x-button>Save Changes</x-button>Benefits:
- Readable and clean syntax
- Reusable layouts and components
- Automatic security (XSS protection)
- Integration with modern tools like Livewire
Advanced Features
- @once / @pushOnce / @prependOnce: Ensure content like JS scripts is only rendered once
- @class / @style: Conditional CSS classes and inline styles
- @checked / @selected / @disabled / @readonly / @required: Conditionals for form inputs
- @include / @includeIf / @includeWhen / @includeUnless / @includeFirst / @each: Include subviews or collections elegantly
- @verbatim: Escape Blade for large JS blocks
- @php / @use: Embed raw PHP code or import classes/functions/constants
- Anonymous Components: Create reusable components without a PHP class (
resources/views/components/alert.blade.php)
Supercharging Blade with Livewire
Laravel Livewire allows Blade components to be dynamic without writing JavaScript frameworks. This enables reactive interfaces with server-side logic while maintaining the simplicity of Blade templates.
Conclusion:
Blade is a powerful and flexible templating engine that keeps Laravel applications clean, maintainable, and performant. Compared to plain PHP, it provides readable syntax, reusable layouts, components, advanced directives, and modern integrations like Livewire, making it the backbone of Laravel’s view layer in.
Related Answers
Still need help?
Talk to our Laravel experts
We've handled GDPR/CCPA compliance for dozens of EU & US Laravel.
