#10 Layouts Laravel 5.6

Create a directory called layouts inside projectname/resources/views dir and create a layout template file inside of it called app.blade.php and write below codes:

<html>
<head>
    <title>App Name - @yield('title')</title>
</head>
<body>
@section('sidebar')
    This is the master sidebar.
@show

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

The above template can be used in any view file using a syntax

@extends( 'layoutdirname/layoutfilename' )
e.g.
@extends( layout/app ) // if your layout template called app.blade.php exists inside projectname/resources/views/layout

 The @section directive, defines a section of content meaning its used to inject content while the @yield directive is used to display the contents of a given section ( shown above ) 

#Syntax
@section( 'name' )
  content goes here
  @endsection
// e.g. So lets create a view file called about.blade.php inside the projectname/resources/views/
@extends( layout/app ) // will include the content from app.blade.php layout file.

@section( 'title' )
  Title name             // This will insert 'Title name' in the place where we have used @yield('title') in layout file app.blade.php
  @endsection
  
@section( 'content' )
  This is my Content  // This will insert 'This is my Content' in the place where we have used @yield('content') in layout file app.blade.php
  @endsection  
@include( 'path/filename'); // will include a file , DONT INCLUDE THE FILE EXTENTION '.blade.php' , just foldername.file e.g. layouts.index

Add your review



Leave a Reply