Home /Laravel /Template inheritance In Blade

Template inheritance In Blade

In a nutshell, template inheritance allows us to define a master layout with elements that are common to all web pages. The individual pages extend the master layout. This saves us time of repeating the same elements in the individual pages

Master layout

All blade templates must be saved with the .blade extension. In this section, we are going to create a master template that all pages will extend. The following is the syntax for defining a master layout.

  1.  create a new directory layout in /resources/views/
  2. create a new file master.blade.php in /resources/views/layouts/master.blade.php
  3. add the following code to master.blade.php
<html>

<head>

<title>@yield(‘title’)</title>

</head>

<body>

@section(‘sidebar’)

This is the master sidebar.

@show

<div class=”container”>

@yield(‘content’)

</div>

</body>

</html>

HERE,

  • @yield('title') is used to display the value of the title
  • @section('sidebar') is used to define a section named sidebar
  • @show is used to display the contents of a section
  • @yield('content') is used to display the contents of content

Leave a Reply