9 Laravel 11 Basics: Blade Templates

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 code in your templates. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.
Blade template files use the .blade.php file extension and are typically stored in the resources/views directory. Blade views may be returned from routes or controllers using the global view helper. For example:
Route::get('/', function () {
return view('greeting', ['name' => 'Finn']);
});Displaying Data
You can display data passed to your Blade views by wrapping the variable in curly braces. For example, given the following route:
Route::get('/', function () {
return view('welcome', ['name' => 'Samantha']);
});You can display the contents of the name variable like so:
Hello, {{ $name }}.Blade's {{ }} echo statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. You are not limited to displaying the contents of variables passed to the view; you can also echo the results of any PHP function:
The current UNIX timestamp is {{ time() }}.Blade Directives
In addition to template inheritance and displaying data, Blade provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a clean and terse way of working with PHP control structures while remaining familiar to their PHP counterparts.
If Statements
You can construct if statements using the @if, @elseif, @else, and @endif directives:
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endifBlade also provides an @unless directive as a convenient alternative to @if statements:
@unless (Auth::check())
You are not signed in.
@endunlessLoops
Blade provides simple directives for working with PHP's loop structures:
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
This is user {{ $user->id }}
@endforeach
@forelse ($users as $user)
{{ $user->name }}
@empty
No users
@endforelseThe $loop variable is available inside your loop and provides useful information such as the current loop index and whether it is the first or last iteration:
@foreach ($users as $user)
@if ($loop->first)
This is the first iteration.
@endif
@if ($loop->last)
This is the last iteration.
@endif
This is user {{ $user->id }}
@endforeachComponents
Blade components and slots provide similar benefits to sections, layouts, and includes but offer a more streamlined approach. You can create a class-based component using the make:component Artisan command:
php artisan make:component AlertThis command creates a component in the app/View/Components directory and a corresponding Blade view in the resources/views/components directory. To use the component, you can reference it in your Blade template:
<x-alert type="danger" message="This is an alert!" />Conclusion
Blade is a versatile and efficient templating engine that leverages the full power of PHP while providing a clean and readable syntax. Whether you're displaying data, controlling the flow of your application, or creating reusable components, Blade makes it easy to build dynamic and responsive web applications with Laravel 11.
For more detailed information, refer to the official Laravel Blade documentation.
Learn more about Blade templates and how they can enhance your Laravel applications. Check out the official documentation for more examples and advanced usage.
Discuss Your Project with Us
We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.
Let's find the best solutions for your needs.