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.
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 is true
.'class-if-false'
– Class that will be applied if the condition is false
.<div class="{{ $user->is_admin ? 'admin-class' : 'regular-user-class' }}"> <!-- Content goes here --> </div>
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,
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.
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
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>
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.
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!”.
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>
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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More