Blade Template in Laravel




Blade is the templating engine used by Laravel, allowing you to create views (HTML files) with the flexibility of including PHP code within them. Blade provides a clean, simple, and expressive way to work with views, enabling you to create layouts, include partials, use conditional statements, loops, and much more.

Source Code

laravel-blade\routes\web.php
<?php
 
use Illuminate\Support\Facades\Route;
 
Route::get('/', function () {
    return view('welcome');
});
 
Route::get('/about',function(){
    return view('about');
});
 
Route::get('/loop',function(){
    return view('loop');
});
 
Route::get('/conditional',function(){
    return view('conditional');
});
Laravel-routes\views\welcome.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>Laravel Blade Tutor Joes</h1>
    <p>{{ 10+2 }}</p>

    <p>{{ "Tutor Joes" }}</p>

    {{ "<h1>Welcome to Laravel</h1>" }}
    {!! "<h1>Welcome to Laravel</h1>" !!}

    {{-- <p>{{ 10+2 }}</p> --}}

    <?php 
        $msg="Welcome to Laravel";
        echo "<p>$msg</p>";
    ?>

    @php 
         $msg="Welcome to Laravel";  
    @endphp
    <p>{{ $msg }}</p>

    @php
        $age=25;
    @endphp

    <h2>IF Statement</h2>

    @if ($age>=18)
        <p>You are eligible</p>
    @endif

    <h2>IF ELSE Statement</h2>
    @if ($age>=18)
         <p>You are eligible</p>
    @else
         <p>You are not eligible</p>
    @endif


    <h2>ELSE IF Statement</h2>
    @php
        $a=0;
    @endphp

    @if ($a>0)
       <p>{{ $a }} is +ive Number</p>
    @elseif ($a<0)
     <p>{{ $a }} is - ive Number</p>
    @else
        <p>Zero</p>
    @endif
    
    <h3>Count</h3>

    @php
        $data=[];
    @endphp

    @if (count($data)>0)
        <p>Data Available</p>
    @else
        <p>Data Not Available</p>
    @endif

    <h2>Switch</h2>
    @php
        $dayOfWeek = 'Friday';
    @endphp
    @switch($dayOfWeek)
        @case('Monday')
                <p>It's the beginning of the week. Stay motivated!</p>
            @break
        @case('Wednesday')
                <p>It's the middle of the week. Keep going!</p>
            @break
        @case('Friday')
                <p>It's Friday! The weekend is almost here. Enjoy!</p>
            @break
        @default
        <p>It's a regular day. Keep pushing forward!</p>
    @endswitch


    <h3>unless</h3>
    @unless (false)
         You are not signed in.
    @endunless
    
    <h3>isset & empty</h3>
    @php
        $records=[];
    @endphp

    @isset($records)
        <p>Record is Available</p>
    @endisset

    @empty($records)
        <p>Record is Empty</p>      
    @endempty


</body>
</html>
Laravel-routes\views\about.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>About Page</title>
  <link rel="stylesheet" href="{{ asset('css/style.css') }}">
</head>
<body>
  <h1>About Page</h1>
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Architecto odit atque sit, quisquam illo aut eveniet expedita eos minima maiores maxime veritatis, praesentium, beatae nihil deserunt laudantium nesciunt modi voluptatibus.</p>
  <img src="images/img.jpg"  width="150px">

  <script src="{{ asset('js/script.js') }}"></script>
</body>
</html>
Laravel-routes\views\conditional.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Conditional Classes & Styles</title>
  <style>
    .padding{
      padding: 10px 5px;
    }

    .isActive{
      font-weight: bold;
      color:green;
    }

    .hasError{
      background-color: red;
    }
  </style>
</head>
<body>
  
  @php
    $isActive = true;
    $hasError = false;
  @endphp

  <div  @class(['padding','isActive'=>$isActive,'hasError'=>$hasError])>
    Sample Text
  </div>

</body>
</html>
Laravel-routes\views\loop.blade.php
<!-- nested-array.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loop Statement</title>
</head>
<body>



<h3>While Loop</h3>
@php
  $i=0;
@endphp
@while ($i<6)
    <p>Sample Para {{ $i }}</p>
    @php
        $i++;
    @endphp
@endwhile
<hr>

{{-- ---------------------------------------------------------------------- --}}

<h3>Foreach Loop</h3>
@php
    $users=["Ram","Sam","Raja","Kumar","Ravi"];
@endphp
    
<ul>
    @foreach ( $users as $user)
        <li>{{ $user }}</li>
    @endforeach
</ul>
<hr>

{{-- ---------------------------------------------------------------------- --}}

<h3>For Loop</h3>
<ul>
  @for($i = 1; $i <= 5; $i++)
      <li>Item {{ $i }}</li>
  @endfor
</ul>
<hr>

{{-- ---------------------------------------------------------------------- --}}

<h3>forelse Loop</h3>
@php
$products=["Apple","Orange"];
@endphp

<ul>
  @forelse ($products as $product)
      <li>{{ $product }}</li>
  @empty
      <li>No Products Found</li>
  @endforelse
</ul>
<hr>
{{-- ---------------------------------------------------------------------- --}}

<h3>Loop Variables </h3>
<ul>
  <li>$loop->index</li>
  <li>$loop->iteration</li>
  <li>$loop->first</li>
  <li>$loop->last</li>
  <li>$loop->odd</li>
  <li>$loop->even</li>
  <li>$loop->count</li>
  <li>$loop->remaining</li>
  <li>$loop->parent</li>
</ul>
<hr>

{{-- ---------------------------------------------------------------------- --}}
<h3>$loop->index</h3>
<ul>
  @foreach ( $users as $user)
      <li>{{ $loop->index }} {{ $user }}</li>
  @endforeach
</ul>
<hr>
{{-- ---------------------------------------------------------------------- --}}
<h3>$loop->iteration</h3>
<ul>
  @foreach ( $users as $user)
      <li>{{ $loop->iteration }} {{ $user }}</li>
  @endforeach
</ul>
<hr>

<h3>$loop->count</h3>
<ul>
  @foreach ( $users as $user)
      <li>{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
  @endforeach
</ul>
<hr>

{{-- ---------------------------------------------------------------------- --}}
<h3>$loop->first  | $loop->last</h3>
<ul>
  @foreach ( $users as $user)
      @if($loop->first)
          <li style="color:green">
            {{ $loop->iteration }} / {{ $loop->count }} {{ $user }}
          </li>
      @elseif($loop->last)
          <li style="color:red">
            {{ $loop->iteration }} / {{ $loop->count }} {{ $user }}
          </li>
      @else
          <li>
            {{ $loop->iteration }} / {{ $loop->count }} {{ $user }}
          </li>
      @endif
  @endforeach
</ul>
<hr>
{{-- ---------------------------------------------------------------------- --}}
<h3>$loop->odd  | $loop->even</h3>
<ul>
  @foreach ( $users as $user)
      @if($loop->odd)
          <li style="color:green">
            {{ $loop->iteration }} / {{ $loop->count }} {{ $user }}
          </li>
      @elseif($loop->even)
          <li style="color:red">
            {{ $loop->iteration }} / {{ $loop->count }} {{ $user }}
          </li>
      @endif
  @endforeach
</ul>
<hr>
{{-- ---------------------------------------------------------------------- --}}
<h3>$loop->parent</h3>
<ul>
    @php
        $items = [
            'Biscuit' => [
                'britannia' => ['Marie Gold', 'Little Hearts','Bourbon'],
                'parle' => ['Parle-G', 'Monaco']
            ],
            'Natural' => [
                'fruits' => ['Apple', 'Orange'],
                'Vegetables' => ['Cucumber', 'Pumpkin']
            ]
        ];
    @endphp

    @foreach($items as $item => $value)
        <li>{{ $loop->iteration }}.{{ $item }}
            <ul>
                @foreach($value as $item_second => $value_second)
                    <li>{{ $loop->parent->iteration }}.{{ $loop->iteration }} {{ $item_second }}
                        <ul>
                            @foreach($value_second as $value_third)
                                <li> 
                                    {{ $loop->parent->parent->iteration }}.
                                    {{ $loop->parent->iteration }}.
                                    {{ $loop->iteration }} 
                                    {{ $value_third }}
                                </li>
                            @endforeach
                        </ul>
                    </li>
                @endforeach
            </ul>
        </li>
    @endforeach
</ul>
<hr>
{{-- ---------------------------------------------------------------------- --}}
<h3>continue |  break</h3> 
@php
    $users = [
      (object)['name' => 'Joes', 'type' => 2, 'number' => 3],
      (object)['name' => 'Ram', 'type' => 1, 'number' => 2],
      (object)['name' => 'Kumar', 'type' => 2, 'number' => 5],
    ];
@endphp

@foreach ($users as $user)
    @if ($user->type == 1)
        @continue
    @endif
    
    <li>{{ $user->name }}</li>

    @if ($user->number == 5)
        @break
    @endif
@endforeach


</body>
</html>