Blade Layouts

Share a common layout across pages with @extends and @section.

Syntax@extends('layouts.app')

Most sites share a header and footer. Define a base layout with @yield placeholders, then have each page @extends it and fill in @section blocks. Modern Blade also offers reusable components.

Example

Example · php
<!-- child page -->
@extends('layouts.app')

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

When to use it

  • A marketing website defines a single master.blade.php layout with the common HTML shell, and every page template uses @extends('master') to inherit it.
  • A dashboard uses a named Blade component <x-app-layout> as the outer shell, injecting the main content through a named slot.
  • A multi-section page uses @section and @push to let child views add content to the footer scripts stack without modifying the parent layout.

More examples

Define a layout with @yield

The master layout uses @yield to mark injection points for child views and @stack for content pushed from child templates.

Example · php
{{-- resources/views/layouts/app.blade.php --}}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>@yield('title', 'My App')</title>
    @stack('styles')
</head>
<body>
    @include('partials.nav')
    <main>@yield('content')</main>
    @include('partials.footer')
    @stack('scripts')
</body>
</html>

Extend the layout in a child view

The child view @extends the layout, fills the @yield('content') section, and pushes a page-specific script via @push('scripts').

Example · php
{{-- resources/views/posts/index.blade.php --}}
@extends('layouts.app')

@section('title', 'All Posts')

@section('content')
    @foreach ($posts as $post)
        <article>
            <h2>{{ $post->title }}</h2>
            <p>{{ Str::limit($post->body, 120) }}</p>
        </article>
    @endforeach
@endsection

@push('scripts')
    <script src="/js/posts.js"></script>
@endpush

Anonymous Blade component as layout

An anonymous component used as a layout receives page content through $slot (default) and named slots like :title, replacing the older @extends pattern.

Example · php
{{-- resources/views/components/app-layout.blade.php --}}
<html>
<body>
    <nav>...</nav>
    <main>{{ $slot }}</main>
</body>
</html>

{{-- Any page --}}
<x-app-layout>
    <x-slot:title>Products</x-slot:title>
    <h1>Products</h1>
    <p>Browse our catalogue.</p>
</x-app-layout>

Discussion

  • Be the first to comment on this lesson.