Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 12 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Using Blade Layouts and Sections: A Beginner's Guide

Learn how to use Blade layouts and sections to create a DRY, consistent UI for your Laravel application. Stop repeating code and master template inheritance.

LaravelBladeTemplatingLayoutsWeb Developmentphpbackend

Previously in this course, we explored introduction to blade templating to output dynamic data. While that taught us the basics, you likely noticed that copying your HTML boilerplate into every new view is inefficient and error-prone. In this lesson, we solve that by using layouts and sections to build a consistent, maintainable structure for your Task Manager.

The Problem: Why We Need Layouts

If you have five different pages in your application—like a dashboard, a task list, and a profile page—each one probably shares the same HTML head, navigation bar, and footer. Without layouts, you’d have to update every single file just to change a link in your header.

Laravel’s Blade templating engine provides a powerful feature called template inheritance. This allows you to define a "master" skeleton and only swap out the specific content for each page.

Creating Your First Layout

A layout is simply a standard Blade file that acts as a wrapper. Instead of writing the full <html> structure in every view, you define it once in resources/views/layouts/app.blade.php.

Within this file, you use the @yield directive to create "holes" where your child views can inject their unique content.

HTML
<!-- resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
style="color:#808080"><style="color:#4EC9B0">html lang="en">
style="color:#808080"><style="color:#4EC9B0">head>
    style="color:#808080"><style="color:#4EC9B0">title>Task Manager | @yield('title')style="color:#808080"></style="color:#4EC9B0">title>
style="color:#808080"></style="color:#4EC9B0">head>
style="color:#808080"><style="color:#4EC9B0">body>
    style="color:#808080"><style="color:#4EC9B0">nav>...style="color:#808080"></style="color:#4EC9B0">nav>

    style="color:#808080"><style="color:#4EC9B0">main>
        @yield('content')
    style="color:#808080"></style="color:#4EC9B0">main>

    style="color:#808080"><style="color:#4EC9B0">footer>...style="color:#808080"></style="color:#4EC9B0">footer>
style="color:#808080"></style="color:#4EC9B0">body>
style="color:#808080"></style="color:#4EC9B0">html>

The @yield('title') and @yield('content') directives tell Blade: "This is a placeholder. If a child view provides content for this specific name, render it here."

Extending a Layout in Child Views

Now that you have a master skeleton, you can "extend" it in your specific views. This is where sections come into play.

Let's say you are building your task index page. Instead of writing the full HTML boilerplate, you use the @extends directive at the top of your file to point to the layout, and then use @section to fill the placeholders you defined earlier.

HTML
<!-- resources/views/tasks/index.blade.php -->
@extends('layouts.app')

@section('title', 'My Tasks')

@section('content')
    style="color:#808080"><style="color:#4EC9B0">h1>Task Liststyle="color:#808080"></style="color:#4EC9B0">h1>
    style="color:#808080"><style="color:#4EC9B0">p>Here are your current tasks!style="color:#808080"></style="color:#4EC9B0">p>
@endsection

Here is exactly what is happening:

  1. @extends('layouts.app') tells Laravel to wrap this file inside resources/views/layouts/app.blade.php.
  2. @section('title', 'My Tasks') is a shorthand for injecting a short string into the @yield('title') slot.
  3. @section('content') ... @endsection wraps a larger block of code that fills the @yield('content') slot.

Hands-on Exercise

To advance our Task Manager project, follow these steps:

  1. Create the resources/views/layouts directory.
  2. Create app.blade.php inside it and add the basic HTML boilerplate shown in the "Creating Your First Layout" section.
  3. Open your existing task list view and refactor it to extend this new layout.
  4. Verify that the task list still renders correctly in the browser.

Common Pitfalls

  • Forgetting @endsection: If you forget to close a section with @endsection, Blade will often "swallow" the rest of your page content, leading to a broken layout or missing UI elements.
  • Incorrect Pathing: Remember that @extends uses dot notation relative to the resources/views folder. If your file is in layouts/app.blade.php, you reference it as layouts.app.
  • Yielding in the Wrong Order: Blade renders content in the order it sees it. Ensure your layout is defined before your views try to extend it.

Recap

By using layouts and sections, we move from a collection of fragmented files to a clean, centralized system. This is a fundamental step toward building professional applications. You now have a master template that ensures your site's navigation and branding remain consistent, while your child views handle the unique business logic.

For more advanced templating, you might eventually want to explore Laravel Blade components or create Laravel Blade custom directives to keep your code even drier.

Up next: We will look at how to extract common UI elements like headers and footers into reusable partials using @include.

Previous lessonIntroduction to Blade TemplatingNext lesson Implementing Blade Partials
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Mastering Blade Directives for Loops and Conditionals

Learn how to use Blade directives like @if, @foreach, and @forelse to control your view logic and render dynamic lists in your Laravel applications efficiently.

Read more
LaravelJune 25, 20263 min read

Introduction to Blade: Mastering Laravel's Templating Engine

Learn how to use Blade to render dynamic data in Laravel. Discover how to create .blade.php files, use echo syntax, and implement essential directives.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 12 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelJune 25, 20263 min read

Customizing Validation Error Messages for Better Laravel UX

Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    Coming soon
  • 27

    Understanding CSRF Protection

    Coming soon
  • 28

    Preventing Mass Assignment

    Coming soon
  • 29

    Task Manager: Securing the Application

    Coming soon
  • 30

    Introduction to Route Model Binding

    Coming soon
  • 31

    Updating Existing Records

    Coming soon
  • 32

    Deleting Records

    Coming soon
  • 33

    Using Named Routes

    Coming soon
  • 34

    Task Manager: Completing CRUD Functionality

    Coming soon
  • 35

    Introduction to Database Relationships

    Coming soon
  • 36

    Querying Related Data

    Coming soon
  • 37

    Handling File Uploads

    Coming soon
  • 38

    Using Flash Messages for User Feedback

    Coming soon
  • 39

    Task Manager: Adding Status and Priorities

    Coming soon
  • 40

    Introduction to Artisan Commands

    Coming soon
  • 41

    Debugging with Laravel Tinker

    Coming soon
  • 42

    Understanding Service Providers

    Coming soon
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course