Template Inheritance - Laravel Blade Template
Template inheritance is a feature of Laravel Blade Template that allows you to define a parent template and have child templates inherit its structure and layout. This feature makes it easy to create reusable layout elements across multiple pages, reducing code duplication and enhancing code maintainability.
Syntax
@section('section_name')
// contents of the section
@endsection
@yield('section_name')
Example
master.blade.php
<!doctype html>
<html>
<head>
<title>@yield('title') - My Blog</title>
</head>
<body>
<header>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
<main>
@yield('content')
</main>
<footer>
© 2021 My Blog
</footer>
</body>
</html>
home.blade.php
@extends('master')
@section('title', 'Home')
@section('content')
<h1>Welcome to my blog!</h1>
<p>Here you'll find all my latest articles.</p>
@endsection
Output
The home.blade.php
file extends the master.blade.php
file. This means that the contents of the home.blade.php
file will be inserted into the content
section of master.blade.php
. The resulting HTML will look like this:
<!doctype html>
<html>
<head>
<title>Home - My Blog</title>
</head>
<body>
<header>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
<main>
<h1>Welcome to my blog!</h1>
<p>Here you'll find all my latest articles.</p>
</main>
<footer>
© 2021 My Blog
</footer>
</body>
</html>
Explanation
The master.blade.php
file defines a basic HTML template that includes a header, main content area, and footer. The title
tag is left to be defined by the child template. The yield
directive defines a named section in the template.
The home.blade.php
file uses the extends
directive to specify that it extends the master.blade.php
file. The @section
directive defines the contents of the content
section that is defined in the parent master.blade.php
file.
When the home.blade.php
file is rendered, the content
section will be replaced with the content of the @section
directive defined in that file.
Use
Template inheritance makes it easy to create reusable layout structures across multiple pages. You can define a basic layout structure in a master template, and then extend that template as needed in child templates. Each child template can define its own content, but it will inherit the structure and layout of the master template.
This approach can greatly simplify the development of complex web applications by reducing code duplication and enhancing code maintainability.
Important Points
- Template inheritance is a feature of Laravel Blade Template.
- It allows you to define a parent template that child templates can extend.
- Child templates can define their own content, but they inherit the structure and layout of the parent template.
- Template inheritance reduces code duplication and enhances code maintainability.
Summary
Template inheritance is a powerful feature of Laravel Blade Template that allows you to define a master template and have child templates inherit its structure and layout. This feature greatly simplifies the development of complex web applications, reducing code duplication and enhancing code maintainability.