Laravel Blade Template Inheritance, Includes, and Control Structures Tutorial

This article explains how to use Laravel's Blade templating engine for creating reusable layouts with inheritance, including child views, passing parameters, template includes, and Blade's convenient control structures such as conditionals, authentication checks, switch statements, and various loop directives, all illustrated with complete code examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Laravel Blade Template Inheritance, Includes, and Control Structures Tutorial

Blade is Laravel's lightweight yet powerful templating engine that compiles .blade.php files into native PHP, caches them, and imposes virtually no performance overhead.

Template Inheritance : Create a master layout at resources/views/layouts/master.blade.php containing the HTML skeleton and a @yield('content') placeholder. Example:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $title ?? 'Default Title' }}@yield('title')</title>
</head>
<body>
    <div class="container">
        @yield('content')
    </div>
</body>
</html>

Define a child view resources/views/home.blade.php that extends the master layout and fills the sections:

@extends('layouts.master')

@section('title', '我是子页面')

@section('content')
    <p>This is my body content.</p>
@endsection

Register a route to render the view:

Route::get('home', function () {
    return view('home');
});

Parameter Passing : Pass data from the route to the view and display it with {{ $content }} inside the child template.

Route::get('home', function () {
    return view('home', ['content' => '我的动态内容,。。。。']);
});

{{-- In home.blade.php --}}
@section('content')
    <p>{{ $content }}</p>
@endsection

Template Includes : Create reusable header and footer partials ( header.blade.php, footer.blade.php) and include them in a child view with @include('header') and @include('footer').

@extends('layouts.master')
@section('title', '我是子页面')
@section('content')
    @include('header')
    <p>This is my body content.</p>
    @include('footer')
@endsection

Control Structures : Blade provides shortcuts for PHP control flow. Examples include:

@if(count($records) === 1) … @elseif(count($records) > 1) … @else … @endif
@isset($records) … @endisset

and

@empty($records) … @endempty
@auth('admin') … @endauth

and

@guest('admin') … @endguest
@switch($i) @case(1) … @break @default … @endswitch

Loop directives such as @for, @foreach, @forelse, @while, with support for @continue and @break inside loops.

Within loops, the $loop variable gives useful metadata (index, iteration, first, last, even, odd, depth, parent, etc.) which can be used to customize output.

Overall, the article demonstrates how Blade simplifies view management, reduces duplication, and provides expressive syntax for common PHP constructs, making Laravel development more efficient and maintainable.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendWeb DevelopmentPHPtemplateLaravelBlade
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.