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.
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>
@endsectionRegister 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>
@endsectionTemplate 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')
@endsectionControl 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.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.