Template inheritance helps you reuse common parts of your web pages easily. It saves time and keeps your code neat by sharing layouts.
Template inheritance (@extends, @section, @yield) in Laravel
@extends('layout') @section('sectionName') Content here @endsection @yield('sectionName')
@extends tells the view which layout to use as the base.
@section defines a part of the page content that fills a placeholder.
@extends('master') @section('title') Home Page @endsection @section('content') Welcome to the home page! @endsection
<!-- In master.blade.php --> <html> <head> <title>@yield('title')</title> </head> <body> @yield('content') </body> </html>
The master.blade.php file is the main layout with placeholders for title and content. The home.blade.php file extends this layout and fills those placeholders with page-specific content.
<!-- resources/views/layouts/master.blade.php --> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@yield('title')</title> </head> <body> <header> <h1>My Website</h1> </header> <main> @yield('content') </main> <footer> <p>Ā© 2024 My Website</p> </footer> </body> </html> <!-- resources/views/home.blade.php --> @extends('layouts.master') @section('title') Home Page @endsection @section('content') <p>Welcome to the home page!</p> @endsection
Always name your sections clearly to avoid confusion.
You can have multiple @section blocks in one view.
If a section is not defined in the child view, the @yield in the layout will be empty.
Template inheritance lets you build a base layout and fill parts of it in child views.
Use @extends to pick the base layout, @section to add content, and @yield to show it.
This keeps your code DRY (Don't Repeat Yourself) and easy to maintain.
Practice
@extends directive in Laravel Blade templates?Solution
Step 1: Understand the role of
The@extends@extendsdirective tells Laravel which base layout file the current view should build upon.Step 2: Differentiate from other directives
@sectiondefines content blocks,@yielddisplays them, and including partials uses@include. Only@extendssets the base layout.Final Answer:
To specify which base layout the current view should use -> Option CQuick Check:
@extends= base layout [OK]
@extends picks the base layout file [OK]- Confusing
@extendswith@section - Thinking
@extendsoutputs content - Mixing
@extendswith@include
Solution
Step 1: Recall syntax for defining sections
In Blade,@section('name')starts a content block, and@endsectionends it.Step 2: Identify correct usage
@yieldis for displaying sections,@extendssets layout, and@includeinserts partials. Only@sectiondefines content.Final Answer:
@section('title') ... @endsection -> Option AQuick Check:
Define section =@section('name')[OK]
@section('name') to start content blocks [OK]- Using
@yieldto define content - Trying to use
@extendsas a section - Confusing
@includewith section definition
layouts/app.blade.php:
<html>
<head>
<title>My Site - @yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
And a child view:
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<h1>Welcome!</h1>
@endsection
What will be the rendered HTML output?Solution
Step 1: Understand
The layout uses@yieldand@sectioninteraction@yield('title')and@yield('content')to display content from child sections.Step 2: Check child view sections
The child view defines@section('title', 'Home Page')and a content section with an <h1> tag. These replace the yields in the layout.Final Answer:
The rendered HTML includes the title 'Home Page' and the <h1>Welcome!</h1> content -> Option DQuick Check:
@yieldshows@sectioncontent [OK]
@yield outputs content from matching @section [OK]- Expecting raw
@sectiontags in output - Missing that
@section('title', 'text')sets inline content - Confusing
@yieldwith@sectiondefinition
@extends('layouts.master')
@section('content')
<p>Hello World</p>
What is the problem with this code?Solution
Step 1: Check section syntax completeness
The@section('content')directive must be closed with@endsectionto mark the end of the content block.Step 2: Identify missing closing directive
The code snippet lacks@endsection, causing a syntax error or unexpected behavior.Final Answer:
Missing@endsectionto close the content section -> Option AQuick Check:
Always close@sectionwith@endsection[OK]
@section with @endsection [OK]- Forgetting to close
@section - Thinking
@yielddefines content - Believing HTML is not allowed inside sections
@yield placeholders: title, header, and content. You want to create a child view that fills only content and uses a default title and header from the layout. How should you write the child view?Solution
Step 1: Understand default content in layout
If the base layout provides default content fortitleandheader, you don't need to override those sections in the child view.Step 2: Write child view with only needed sections
Only define thecontentsection in the child view to fill that placeholder, leaving others to default.Final Answer:
Child view extends layout and defines onlycontentsection -> Option BQuick Check:
Define only needed sections; others use layout defaults [OK]
- Overriding sections unnecessarily
- Using
@yieldinside child views - Trying to include sections with
@include
Ready to prove it?
Pass the test and this pattern moves unknown
