What if you could change your website's header once and see it update everywhere instantly?
Why Template inheritance (@extends, @section, @yield) in Laravel? - Purpose & Use Cases
Imagine building a website where every page needs the same header, footer, and sidebar. You copy and paste the same HTML into every file. Then, you want to change the header color. You have to open every file and update it one by one.
This manual approach is slow and risky. You might miss some files, causing inconsistent layouts. It's hard to keep the design uniform, and updating common parts becomes a frustrating chore.
Template inheritance lets you define a base layout once, then create pages that fill in only the unique parts. Using @extends, @section, and @yield, Laravel automatically combines your layouts, so changes to the base affect all pages instantly.
<!DOCTYPE html> <html> <head><title>Page 1</title></head> <body> <header>My Site</header> <p>Content for page 1</p> <footer>Footer info</footer> </body> </html>
@extends('base') @section('content') <p>Content for page 1</p> @endsection
This makes building and maintaining websites faster, cleaner, and less error-prone by reusing layouts effortlessly.
Think of a blog where every post shares the same header and footer. With template inheritance, you update the header once, and all posts reflect the change immediately.
Copy-pasting layouts is slow and error-prone.
Template inheritance centralizes common parts for easy updates.
@extends, @section, and @yield work together to build pages efficiently.
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
