in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a templating engine in Laravel that helps developers create dynamic views. Here, you can pass variables, add conditional views, and perform various operations on variables to present views.
Let’s discuss common practices on the Laravel Blade Template.
Laravel blade conditional class
You can use conditional statements to apply classes conditionally into the Laravel template. The sample example of a Laravel blade:
<div class="{{ $condition ? 'class-if-true' : 'class-if-false' }}"> <!-- Content goes here --> </div>
The Parameters:
$condition
– Determines whether the class should be applied.'class-if-true'
– Class will be applied if the condition istrue
.'class-if-false'
– Class that will be applied if the condition isfalse
.
<div class="{{ $user->is_admin ? 'admin-class' : 'regular-user-class' }}"> <!-- Content goes here --> </div>
How To USe isset Directive Laravel blade
Laravel has @isset
inbuilt directive to check if a variable is set and is not null in the template. This can be useful for avoiding errors when trying to access properties or methods on an object that might not exist.
The Syntax:
@isset($admin) <p>The variable is set and is not null.</p> @else <p>The variable is not set or is null.</p> @endisset
in the above code, We have define $admin
varaible and check if the variable is set and is not null, the content inside @isset
will be displayed; otherwise, the content inside @else
will be displayed.
Example:
@isset($salary) <p>{{ $salary }}</p> @else <p>NA</p> @endisset
Also checkout other Laravel 10 tutorials,
- How To Display HTML Tags In Blade With Laravel 8
- Laravel Layout Using Bootstrap
- How to Use Laravel Theme Using Blade
Laravel blade check file exists
The Laravel has file_exists
function to check if a file exists before attempting to include or display it. A simple example:
@if(file_exists(public_path('path/to/your/file'))) <p>The file exist </p> @else <p>File does not exist.</p> @endif
in this example, We have passed the full path of file into the method file_exists()
and checks if the file exists or not at the specified path.
Laravel blade else if Condition
You can achieve ifelse
statements using the @if
, @elseif
, @else
, and @endif
directives. The sample example:
@if($status == 'active') <p>The account is active.</p> @elseif($status == 'pending') <p>The account is pending approval.</p> @elseif($status == 'disabled') <p>The account is disabled.</p> @else <p>The account status is unknown.</p> @endif
laravel blade for loop
You can use the @for
directive to create a for loop
. Here’s the basic syntax:
@for($i = 0; $i < 5; $i++) <p>{{ $i }}</p> @endfor
In this example, the loop starts with $i
set to 0, continues as long as $i
is less than 5, and increments $i
by 1 in each iteration.
Example:
<ul> @for($i = 1; $i <= 3; $i++) <li>Item {{ $i }}</li> @endfor </ul>
Laravel blade nested if
You can also define nested if else
statement in to Laravel the blade template.
@if($user->isAdmin()) <p>Welcome, Admin!</p> @if($user->hasPermission('manage_users')) <p>You can manage users.</p> @endif @else <p>Welcome, User!</p> @endif
In the above example, if the user is an admin, it displays a welcome message for admins. If the admin has the permission to manage users, it also displays a message about managing users. If the user is not an admin, it displays a welcome message.
How To Use ternary Operator in Laravel blade
You can use the ternary operator within HTML to conditionally output content. The basic syntax is:
{{ $condition ? 'Value if true' : 'Value if false' }}
Example:
<p> {{ $user->isAdmin() ? 'Welcome, Admin!' : 'Welcome, User!' }} </p>
In this example, if $user->isAdmin()
returns true, it outputs “Welcome, Admin!”, otherwise, it outputs “Welcome, User!”.
Laravel blade redirect
You can page redirection by using PHP backend side or you can use javascript to redirect a page from laravel blade template, but it’s not a common way to redirect:
<script> setTimeout(function() { window.location.href = 'example-url'; }, 3000); // Redirect after 3 seconds </script>
Conclusion:
We’ve covered a variety of common techniques for Laravel Blade templates. We have explored blade directives, including @if
, @else
, @for
, and @ifelse
. Developers are able to design optimized, effective, and easily maintained user interfaces with the help of the Blade template engine.