Lesson 12 of 50 – Blade Template Engine
24%

Laravel Blade Template Engine

Blade is Laravel's built-in templating engine. It allows you to display dynamic data, write conditions and loops, reuse templates, and create clean HTML pages.

1. What Is Blade?

Blade is Laravel's template engine for creating dynamic HTML pages. Blade templates usually have the .blade.php extension.

Example file:

resources/views/home.blade.php

Example Blade template:

<h1>Welcome to Laravel</h1>

<p>Learn PHP with Soopro Pathshala.</p>

Blade templates can contain HTML, Blade directives, and PHP expressions. Laravel processes the template and returns the resulting HTML to the browser.

2. Display Variables Using Blade

Blade uses double curly braces to display a variable.

<h1>{{ $name }}</h1>

<p>Course: {{ $course }}</p>

Suppose your controller passes these variables:

$name = 'Rahul';
$course = 'Laravel';

return view('student', compact('name', 'course'));

The output will be:

Rahul

Course: Laravel

Blade's {{ }} syntax escapes output by default, helping protect against HTML injection.

3. Conditional Statements in Blade

Use @if, @elseif, @else, and @endif to display content based on conditions.

@if ($marks >= 40)
    <p>Result: Pass</p>
@else
    <p>Result: Fail</p>
@endif

Example with multiple conditions:

@if ($marks >= 80)
    <p>Grade: A</p>
@elseif ($marks >= 60)
    <p>Grade: B</p>
@elseif ($marks >= 40)
    <p>Grade: C</p>
@else
    <p>Grade: Fail</p>
@endif

Blade evaluates the conditions and renders the matching section.

4. Loops Using @foreach

Use @foreach to display items from an array or collection.

Blade example:

<h2>Available Courses</h2>

<ul>
    @foreach ($courses as $course)
        <li>{{ $course }}</li>
    @endforeach
</ul>

Example data passed from a controller:

$courses = [
    'PHP',
    'Laravel',
    'JavaScript',
    'React'
];

return view('courses', compact('courses'));

Output:

  • PHP
  • Laravel
  • JavaScript
  • React
5. @forelse and Empty Lists

The @forelse directive loops through items and provides an alternative when the list contains no items.

<h2>Student List</h2>

<ul>
    @forelse ($students as $student)
        <li>{{ $student }}</li>
    @empty
        <li>No students found.</li>
    @endforelse
</ul>

If the collection is empty, Laravel displays the message inside @empty.

You can also use @isset to check whether a variable exists and is not null.

@isset($name)
    <p>Student Name: {{ $name }}</p>
@endisset
6. Blade Comments and PHP Code

Blade supports comments that are removed when the template is rendered.

{{-- This is a Blade comment --}}

<h1>Welcome to Laravel</h1>

Unlike HTML comments, Blade comments are not included in the rendered HTML output.

Blade also supports the @php directive for small amounts of PHP code.

@php
    $name = 'Amit';
    $course = 'Laravel';
@endphp

<p>Name: {{ $name }}</p>
<p>Course: {{ $course }}</p>

For larger operations or business logic, prefer controllers or dedicated classes rather than putting complex PHP code in a view.

7. Display Raw HTML Safely

Blade's double curly braces escape HTML by default. This is the recommended choice for most dynamic data.

{{ $message }}

If $message contains HTML, the tags will normally be displayed as text rather than rendered as HTML.

Blade also provides unescaped output syntax:

{!! $html !!}

Security warning: Use {!! !!} only when the HTML is trusted or has been properly sanitized. Rendering untrusted user input this way can introduce cross-site scripting (XSS) vulnerabilities.

8. Include Another Blade View

The @include directive lets you reuse a Blade view inside another view.

Create:

resources/views/header.blade.php

Inside header.blade.php:

<header>
    <h2>Soopro Pathshala</h2>
    <hr>
</header>

Use it inside home.blade.php:

@include('header')

<h1>Welcome to the Home Page</h1>

<p>Learn Laravel step by step.</p>

Laravel renders the included view at that location. You can also pass data to an included view.

@include('header', ['title' => 'Laravel Tutorial'])

Inside the included view, the $title variable is available.

9. Practical Assignment

Complete these exercises to practise Blade templates.

  1. Create a Blade view that displays a welcome message.
  2. Display a student's name and course using variables.
  3. Use @if to show Pass or Fail based on marks.
  4. Display five course names using @foreach.
  5. Use @forelse to display an empty-list message.
  6. Add a Blade comment to your template.
  7. Use @php to define a simple variable.
  8. Create a reusable header view and include it with @include.
  9. Compare escaped output {{ }} with raw output {!! !!}.

Remember to keep complex business logic in controllers and use Blade primarily for presentation.


🧠 Quick Quiz

1. What is the file extension of a Blade template?




2. Which directive loops through an array?




3. Which syntax escapes output by default?