Back to results for “Architecture”
PHP › Template Engines (Blade & Twig)

How does template inheritance work in Blade and Twig, allowing you to define a shared base layout that individual pages can extend?

Intermediate

Answer

Template inheritance lets you define a single base layout template containing shared structural elements common to every page, such as the header, navigation menu, and footer, along with one or more named sections left intentionally empty as placeholders, and individual page templates then extend that base layout, filling in only the specific content unique to that particular page within those designated placeholder sections, which avoids duplicating the same shared layout markup across every single page in an application.
{{-- Blade: layout.blade.php --}}
<html><body>@yield('content')</body></html>

{{-- Blade: page.blade.php --}}
@extends('layout')
@section('content')
    <p>Page content here</p>
@endsection
Real-world example A large website defines a single shared base layout containing its consistent header and footer, with every individual page template extending that layout and only providing its own unique main content, avoiding the need to repeat the same header and footer markup across dozens of separate page files.

Common follow-ups: Can a template extend more than one parent layout at the same time?;How do Blade components differ from traditional template inheritance for reusing markup?