PHP Development | Tutorial 12 | Blade Templating in Laravel

by | Oct 8, 2025 | PHP Development, Web App Development | 0 comments

Introduction

In web applications, separating logic from presentation is crucial for clean code and maintainability. Laravel provides Blade, its powerful templating engine, to make this separation seamless. Blade allows developers to write HTML views with embedded logic using simple, elegant syntax.

For web developers and beginners in Laravel, mastering Blade is essential. In interviews, expect questions about directives, layouts, components, and security in Blade. Let’s explore how Blade works and why it’s preferred over plain PHP templates.

What is Blade?

Blade is Laravel’s lightweight templating engine that helps you write dynamic HTML without mixing raw PHP everywhere. Unlike other template engines, Blade does not restrict you from using PHP—it simply adds convenient features like:

  • Directives (@if, @foreach, @csrf)
  • Layouts & Template Inheritance
  • Components & Slots
  • Escaping for security

👉 Blade templates are stored in resources/views and compiled into plain PHP for fast execution.

Blade Syntax Basics

1. Output Variables

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

Escapes HTML by default for security (XSS protection).

Raw output (not escaped):

{!! $html !!}

2. Conditionals

@if($user->isAdmin())
    <p>Welcome, Admin!</p>
@elseif($user)
    <p>Hello, {{ $user->name }}</p>
@else
    <p>Guest User</p>
@endif

Shortcut:

{{ $user->name ?? 'Guest' }}

3. Loops

@foreach($posts as $post)
    <li>{{ $post->title }}</li>
@endforeach

@for($i = 0; $i < 5; $i++)
    <p>Iteration {{ $i }}</p>
@endfor

Special loop variables:

@foreach($posts as $post)
    <p>{{ $loop->iteration }}. {{ $post->title }}</p>
@endforeach

4. Including Views

@include('partials.header')
@include('partials.footer')

👉 Helps reuse layouts across multiple pages.

Blade Layouts & Template Inheritance

Blade supports master layouts and child views, reducing duplication.

Master Layout: resources/views/layouts/app.blade.php

<html>
<head>
    <title>@yield('title', 'My App')</title>
</head>
<body>
    <div class="content">
        @yield('content')
    </div>
</body>
</html>

Child View: resources/views/home.blade.php

@extends('layouts.app')

@section('title', 'Home Page')

@section('content')
    <h1>Welcome to Laravel</h1>
@endsection

👉 This structure is used in nearly all Laravel web applications.

Blade Components

Components allow building reusable UI blocks.

Example: Button Component

php artisan make:component Button

Generated file:

resources/views/components/button.blade.php
<button class="btn btn-primary">
    {{ $slot }}
</button>

Use in a view:

<x-button>Save</x-button>

👉 Output: <button class="btn btn-primary">Save</button>

Security in Blade

  • Escaping by Default → {{ $var }} prevents XSS.
  • @csrf Directive → Protects forms against CSRF attacks.
<form method="POST" action="/submit">
    @csrf
    <input type="text" name="name">
</form>

@auth / @guest → Securely show content only to specific users.

Real-World Use Cases

  • Dashboards → Layout inheritance keeps admin panels DRY.
  • Forms → Blade directives simplify CSRF and validation errors.
  • Reusable UI → Components for buttons, alerts, modals.
  • Conditional Menus → Show/hide links based on roles with @can.

Common Beginner Mistakes

  • Using <?php ?> instead of Blade directives.
  • Forgetting @csrf in forms (leads to 419 error).
  • Hardcoding repeated HTML instead of using components.
  • Printing raw HTML without escaping → XSS vulnerabilities.

Sample Interview Questions & Answers

Q: What is Blade in Laravel?

A: Blade is Laravel’s templating engine that helps render dynamic views with simple syntax, layouts, and components.

Q: What’s the difference between {{ }} and {!! !!} in Blade?

A: {{ }} escapes HTML for security, while {!! !!} renders raw, unescaped HTML.

Q: How does Blade handle layouts?

A: Using @extends, @section, and @yield for template inheritance.

Q: What are Blade components?

A: Reusable UI blocks (like <x-button>) that improve maintainability.

Q: How does Blade prevent CSRF attacks?

A: By using the @csrf directive in forms to generate CSRF tokens.

Q: Can Blade use plain PHP code?

A: Yes, but best practice is to use Blade directives instead of mixing raw PHP.

Mini Project Idea

👉 Build a Blog UI with Blade:

  • Master layout (layouts/app.blade.php).
  • Navigation partial (partials/nav.blade.php).
  • Reusable button component (<x-button>).
  • Blog list view with @foreach to display posts.

Closing Note

Blade is more than just a templating engine—it’s a developer productivity booster that makes Laravel views elegant, secure, and maintainable.

Laravel Framework Mastery

Why Choose Laravel?
→ Discover why Laravel dominates modern PHP development

Laravel Routing & Middleware
→ Control application flow with routes and middleware layers

Laravel MVC Architecture
→ Build scalable apps using Models, Views, and Controllers

Laravel Artisan & Productivity Tools
→ Supercharge your workflow with Artisan commands and dev tools

Advantages of Eloquent ORM
→ Master database operations with Laravel’s powerful ORM

Laravel Collections Explained
→ Simplify data handling with powerful collection methods for filtering, mapping, and transforming arrays effortlessly

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *