Laravel Blade Introduction

Laravel Blade Introduction

Blade is Laravel's lightweight, powerful templating engine. Blade files use the .blade.php extension and are compiled to plain PHP and cached.

1 - Displaying Data

// Escaped output (safe — prevents XSS)
{{ $name }}
{{ $user->email }}
{{ now()->format('Y-m-d') }}

// Unescaped output (use only for trusted HTML)
{!! $htmlContent !!}

2 - Blade Comments

{{-- This comment is not rendered in the HTML output --}}

3 - Passing Data to Views

// From a controller
return view('profile', ['user' => $user]);

// Using compact()
return view('profile', compact('user', 'posts'));

// Using with()
return view('profile')->with('user', $user);

4 - Returning Views from Routes

// Simple view with no data
Route::get('/about', fn () => view('about'));

// With data
Route::get('/welcome', fn () => view('welcome', ['name' => 'World']));

Note: Always use {{ }} for user-supplied data. Never use {!! !!} unless you have explicitly sanitised the value — it outputs raw HTML without escaping.

-Tip-