0Pricing
PHP Academy · Lesson

Blade Templates and Views

Render dynamic HTML with Blade directives, layouts, and components.

Blade Templates and Views is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Blade?

Blade is Laravel' templating engine. Files end in .blade.php and support directives, loops, and layout inheritance. Blade is compiled to plain PHP and cached.

Outputting Variables

Use double curly braces to output a variable. The value is automatically escaped to prevent XSS.

{{-- resources/views/greeting.blade.php --}}
<h1>Hello, {{ $name }}</h1>
<p>You have {{ $messageCount }} messages.</p>

Unescaped Output

Use {!! !!} for raw (unescaped) HTML output. Only use this for trusted content.

{!! $htmlContent !!}

Conditionals

Blade provides @if, @elseif, @else, @endif.

@if($user->isAdmin())
    <a href="/admin">Admin Panel</a>
@elseif($user->isEditor())
    <a href="/editor">Editor</a>
@else
    <a href="/dashboard">Dashboard</a>
@endif

@isset and @empty

Convenient shorthands for common null/empty checks.

@isset($username)
    <p>Welcome, {{ $username }}</p>
@endisset

@empty($items)
    <p>No items found.</p>
@endempty

Loops

Blade supports @foreach, @for, @while, and @forelse (with @empty fallback).

@forelse($posts as $post)
    <article>
        <h2>{{ $post->title }}</h2>
    </article>
@empty
    <p>No posts yet.</p>
@endforelse

The $loop Variable

Inside a @foreach, $loop provides metadata: $loop->index, $loop->first, $loop->last, $loop->count.

Layout with @extends and @section

Define a master layout and extend it in child views.

{{-- layouts/app.blade.php --}}
<html>
<body>
    @yield("content")
</body>
</html>

{{-- pages/home.blade.php --}}
@extends("layouts.app")
@section("content")
    <h1>Home Page</h1>
@endsection

@include

Include a sub-view partial.

@include("partials.nav")
@include("partials.alert", ["type" => "success", "msg" => "Saved!"])

Blade Components

Reusable components live in resources/views/components/ and are used with <x-component-name> syntax.

{{-- components/alert.blade.php --}}
<div class="alert alert-{{ $type }}">
    {{ $slot }}
</div>

{{-- Usage: --}}
<x-alert type="success">Profile updated!</x-alert>

Passing Data to Views

From a controller, pass data using view() with an array or compact().

<?php
return view("users.index", [
    "users" => User::all(),
    "title" => "All Users",
]);
// Or:
return view("users.index", compact("users", "title"));

Summary

Blade compiles to PHP. Use {{ }} for escaped output, @extends/@section for layouts, @foreach/@forelse for loops, and <x-component> for reusable UI.

Quick Check

Which Blade syntax safely escapes output to prevent XSS?

Frequently asked questions

Is the “Blade Templates and Views” lesson free?

Yes — the full text of “Blade Templates and Views” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Blade Templates and Views”?

Render dynamic HTML with Blade directives, layouts, and components. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Blade Templates and Views” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Laravel Project Structure
  2. Defining Routes in Laravel
  3. Controllers and Actions
  4. Blade Templates and Views
← Back to PHP Academy