Blade is Laravel’s built-in templating engine that makes writing clean, dynamic HTML a breeze. With its simple syntax and powerful features like loops, conditionals, and layouts, Laravel Blade helps you build flexible views efficiently.
This guide walks you through how to use Blade for conditional rendering, loops, and variables with real-world examples.
🧠 What is Laravel Blade?
Blade is a lightweight, fast, and expressive templating engine provided by Laravel. It allows you to:
- Embed PHP logic in HTML
- Use simple control structures like
@if,@foreach,@isset - Share layout templates via
@extendsand@section
Unlike traditional PHP views, Blade compiles into plain PHP, so there’s no performance cost.
🛠 Blade Syntax Basics
Here’s what makes Blade syntax special:
| PHP | Blade |
|---|---|
<?php echo $title; ?> | {{ $title }} |
<?php if ($condition): ?> | @if($condition) |
<?php foreach ($users as $user): ?> | @foreach($users as $user) |
🔄 Using Variables in Blade
Displaying Data:
<h1>Welcome, {{ $user->name }}</h1>
Escaping HTML:
Use {{ }} for auto-escaped content and {!! !!} for raw HTML.
{{ $post->title }} <!-- Escaped -->
{!! $post->description !!} <!-- Not escaped (Be careful!) -->
✅ Blade If Conditions
Blade supports all PHP conditional logic:
Basic @if, @elseif, @else:
@if($user->isAdmin())
<p>Welcome Admin!</p>
@elseif($user->isSubscriber())
<p>Thanks for subscribing!</p>
@else
<p>Please sign up.</p>
@endif
Using @isset:
Checks if a variable is set:
@isset($post)
<h2>{{ $post->title }}</h2>
@endisset
Using @empty:
Checks if a variable is empty:
@empty($comments)
<p>No comments yet.</p>
@endempty
🔁 Blade Loops
1. @foreach
<ul>
@foreach($users as $user)
<li>{{ $user->name }}</li>
@endforeach
</ul>
You can access the loop index and other info with $loop:
@foreach($users as $user)
<li>#{{ $loop->iteration }} - {{ $user->name }}</li>
@endforeach
2. @for Loop
@for($i = 0; $i < 5; $i++)
<p>Item {{ $i }}</p>
@endfor
3. @while Loop
@php $count = 1; @endphp
@while($count <= 3)
<p>Count: {{ $count }}</p>
@php $count++; @endphp
@endwhile
4. @forelse (With Empty Check)
@forelse($products as $product)
<li>{{ $product->name }}</li>
@empty
<li>No products available</li>
@endforelse
📦 Blade and Components
Use components to write reusable UI.
<!-- resources/views/components/alert.blade.php -->
<div class="alert alert-{{ $type }}">
{{ $message }}
</div>
Use in views:
<x-alert type="danger" message="Something went wrong!" />
Or:
<x-alert type="success">
Everything is good!
</x-alert>
📁 Blade Layouts and Inheritance
Define a layout in layouts/app.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
Extend it in a view:
@extends('layouts.app')
@section('title', 'Dashboard')
@section('content')
<h1>Welcome to the Dashboard!</h1>
@endsection
🧠 Best Practices for Blade
- ✅ Avoid putting complex logic in views — keep logic in controllers or view models
- ✅ Use components for reusable UI blocks
- ✅ Use
@forelseinstead of@foreach + @empty - ✅ Escape data unless you trust it (
{{ }}vs{!! !!})
📈 SEO Benefits of Using Blade
- Blade lets you easily manage
<title>,<meta>tags via layouts - You can dynamically inject canonical URLs and meta descriptions
- Blade makes rendering structured data (e.g. JSON-LD) easy for rich snippets
