Template Inheritance with @extends, @section, and @yield in Laravel
📖 Scenario: You are building a simple website with a consistent layout. You want to create a main layout template and then create a page that uses this layout. This helps keep your site organized and easy to update.
🎯 Goal: Create a base layout template using @yield for content placeholders. Then create a child template that uses @extends to inherit the layout and fills in content using @section.
📋 What You'll Learn
Create a base layout file named
layout.blade.php with a @yield('content') placeholder inside the <main> tag.Create a child view file named
home.blade.php that uses @extends('layout') to inherit the layout.In
home.blade.php, define a @section('content') that includes a heading <h1>Welcome Home</h1> and a paragraph <p>This is the home page.</p>.End the section with
@endsection.💡 Why This Matters
🌍 Real World
Web developers use template inheritance to build websites with consistent layouts and reusable code, saving time and reducing errors.
💼 Career
Understanding Laravel's Blade template inheritance is essential for backend and full-stack developers working with Laravel to create maintainable web applications.
Progress0 / 4 steps