INT221 – Blade Templates – B.TECH (CSE) LPU Notes

Need help? Call us

0800 900 1000

BLADE TEMPLATES

Introduction

  • Blade is the simple, yet powerful templating engine that is included with Laravel.
  • Blade template files use the .blade.php file extension and are typically stored in the resources/views directory.

Displaying Data

  • You may display data that is 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 may display the contents of the name variable like so:
  • Hello, {{ $name }}.

Blade Directives

  • Blade directives are short PHP snippets that are used to extend the functionality of Blade templates. They are prefixed with an ‘@’ symbol and can be used to perform a variety of tasks, such as:
  • Conditional logic: The @if and @else directives can be used to conditionally render output.
  • Loops: The @foreach and @forelse directives can be used to loop through collections of data.
  • Including other views: The @include directive can be used to include the contents of another view file.
  • Rendering expressions: The @php and @endphp directives can be used to render PHP expressions.

Loops

  • Blade provides simple directives for working with PHP’s loop structures.

@foreach ($users as $user)

<p>This is user {{ $user}}</p>

@endforeach

@forelse ($users as $user)

<li>{{ $user}}</li>

@empty

<p>No users</p> @endforelse

If Statements

  • You may 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!

@endif

If Statements(contd.)

  • In addition to the conditional directives already discussed, the @isset and @empty directives may be used as convenient shortcuts for their respective PHP functions:

@isset($records)

// $records is defined and is not null… @endisset

@empty($records)

// $records is “empty”… @endempty

Switch Statements

  • Switch statements can be constructed using the @switch, @case, @break, @default and @endswitch directives:

@switch($i) @case(1)

First case…

@break

@case(2) Second case… @break

@default

Default case… @endswitch

Now Some Practical of blade directives

  • Step 1 – Make a Controller using Artisan
  • php artisan make:controller UserController
  • Step 2 – Make a route in web.php and don’t forget to include the UserController in web.php
  • Route::get(‘/profile’, [UserController::class,’showProfile’]);
  • Step 3, Now in UserController class let’s define the function showProfile()

public function showProfile()

{

3′];

$username = ‘Mr Stark’;

$isAdmin = true;

$items = [‘Item 1’, ‘Item 2’, ‘Item

return view(‘profile’,

compact(‘username’, ‘isAdmin’, ‘items’));

}

Step 4- define a view in resources/view folder ‘profile.blade.php’

<!DOCTYPE html>

<html>

<head>

<title>User Profile</title>

</head>

<body>

<h1>Welcome to your profile, {{ $username }}</h1>

@if($isAdmin)

<p>You are an administrator.</p> @else

<p>You are not an administrator.</p> @endif

<ul>

@foreach($items as $item)

<li>{{ $item }}</li> @endforeach

</ul>

  • <hr>

<h2>Additional Blade Directives:</h2>

{{– @unless directive –}}

  • @unless($isAdmin)
  • <p>You don’t have admin privileges.</p>
  • @endunless

{{– @empty directive –}}

  • @empty($additionalData)
  • <p>Additional data is empty.</p>
  • @endempty

{{– @for directive –}}

  • <h3>Looping with for</h3>
  • <ul>
  • @for($i = 1; $i <= 3; $i++)
  • <li>Iteration {{ $i }}</li>
  • @endfor
  • </ul>

{{– @include directive –}}

  • {{–<h3>Including Sub-Views:</h3>
  • @include(‘partials.footer’)–}}

{{– Blade Comments –}}

  • {{– This is a Blade comment –}}

</body>

  • </html>

What file extension is typically used for Blade template files in Laravel?

  • a) .php
  • b) .blade.php
  • c) .tpl
  • d) .view

What file extension is typically used for Blade template files in Laravel?

  • a) .php
  • b) .blade.php
  • c) .tpl
  • d) .view

How can you display a variable named

$name in a Blade template?

  • a) Hello, $name.
  • b) Hello, {!! $name !!}.
  • c) Hello, {{ $name }}.
  • d) Hello, {{$name}}.

How can you display a variable named

$name in a Blade template?

  • a) Hello, $name.
  • b) Hello, {!! $name !!}.
  • c) Hello, {{ $name }}.
  • d) Hello, {{$naame}}.

Which symbol is used to prefix Blade directives in Laravel?

  • a) #
  • b) $
  • c) !
  • d) @

Which symbol is used to prefix Blade directives in Laravel?

  • a) #
  • b) $
  • c) !
  • d) @

How do you create a loop in a Blade template to iterate through an array called $users?

  • a) @for ($user in $users)
  • b) @foreach ($user as $users)
  • c) @loop ($users as $user)
  • d) @foreach ($users as $user)

How do you create a loop in a Blade template to iterate through an array called $users?

  • a) @for ($user in $users)
  • b) @foreach ($user as $users)
  • c) @loop ($users as $user)
  • d) @foreach ($users as $user)

Which Blade directive is used to check if a variable is set and not null?

  • a) @isset
  • b) @empty
  • c) @ifset
  • d) @nullcheck

Which Blade directive is used to check if a variable is set and not null?

  • a) @isset
  • b) @empty
  • c) @ifset
  • d) @nullcheck

Which Blade directive is used for executing a loop with a defined number of iterations?

  • a) @while
  • b) @loop
  • c) @for
  • d) @foreach

 

[pdf_note link=”https://drive.google.com/file/d/1x8dt0rH208c-G1Ob63NUz3Hgn21O5XIY/view”]

Leave a Comment