LARAVEL

Laravel Extending the master layout


Extending the master layout in Laravel Blade allows you to create a hierarchy of templates where you can define a main layout and then extend it in other views while providing specific content for different sections.

 

Here's how you extend the master layout in Laravel Blade

 

step 1. Create the Master Layout:

 First, you need to create a master layout file that defines the overall structure of your application. This file typically contains the HTML structure, common headers, footers, and any other sections that you want to include across multiple views.

Example: resources/views/layouts/app.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>@yield('title', 'Your App')</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <header>
        <!-- Header content -->
    </header>

    <div class="container">
        @yield('content')
    </div>

    <footer>
        <!-- Footer content -->
    </footer>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}"></script>
</body>
</html>

 

Step 2. Extend the Master Layout in Views:

 Next, you can extend the master layout in your other views by using the @extends directive. This tells Laravel to use the master layout as the base template for the current view.

Example: resources/views/home.blade.php

@extends('layouts.app')

@section('title', 'Home Page')

@section('content')
    <div class="jumbotron">
        <h1>Welcome to Your Application</h1>
        <p>This is the home page content.</p>
    </div>
@endsection

In the above example:

  • @extends('layouts.app') indicates that the home.blade.php view extends the app.blade.php master layout.
  • @section('title', 'Home Page') defines the title section, which will be used in the master layout's <title> tag.

@section('content') and @endsection delineate the content section, where you place the specific content for this view.
 


LARAVEL