Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Laravel Blade Templates
Laravel Advanced FREE

Blade Templates

Lesson 2 of 17 Advanced Interactive

Blade is Laravel's templating engine with clean syntax.

Directives

  • @extends, @section, @yield � layout inheritance
  • @foreach, @forelse � loops
  • @if, @elseif, @else � conditionals

Syntax

LARAVEL
@extends("layouts.app")
@section("content")
    <h1>{{ $title }}</h1>
    @foreach($posts as $post)
        <div>{{ $post->title }}</div>
    @endforeach
    @if($posts->isEmpty())
        <p>No posts found.</p>
    @endif
@endsection
Blade Templating
PHP
<!-- resources/views/posts/index.blade.php -->
@extends("layouts.app")

@section("content")
@foreach ($posts as $post)
    <article>
        <h2>{{ $post->title }}</h2>
        <p>{{ $post->excerpt }}</p>

        @if ($post->image)
            <img src="{{ $post->image }}" alt="...">
        @else
            <p>No image</p>
        @endif

        <small>{{ $post->created_at->diffForHumans() }}</small>

        @empty
            <p>No posts yet.</p>
    @endforelse
@endsection

Practice

1
Exercise

Create a Blade layout with header, content, and footer sections.

Answer
@extends("layouts.app")
@section("content")
    <h1>{{ $title }}</h1>
@endsection

Quick Quiz

1

What does @yield do?

@yield defines where a section content should be placed.

Interview Questions

Blade provides cleaner syntax, template inheritance, XSS protection via {{ }}, and compiles to PHP for caching.