Generic layout for header and footer

Generic layout for header and footer

For creating a generic layout in Laravel, first of all we need to create a new folder inside the views with the name layouts and create a new file layout.blade.php inside this folder and perform the following steps:

  • Copy all the header and footer content that is same in all the view files from beginning to <body >tag and then close the body and html tag and paste into this file
  • Now to use this external layout file we will use @extends (‘layouts. layout’). Here layouts is folder name and layout is file name. no need to write the.blade.php here.
  • Here we extend the external files which contain starting and ending of html. But it’s a little bit confusing to understand where to put the different template which is unique for all pages.

To resolve this issue, we define this as a specific section of content that help us to distinguish and put this on the right place. For this we use @section(‘content’ and @endsection

Example

@extends(‘layouts.layout’)

@section(‘content’)

<div class=”row”>

    <div class=”card col-md-8 mt-5 mx-auto”>

        <h2> Courses Name and their fee</h2>

    </div>

</div>

Now we use this content section in layout file with different directive @yield and this yield allow us to yield a specific section by passing the name of the section where we want to use this section

@yield(‘content’)

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*