The view file is not a final file that is rendered and generates HTML. We can create subviews also there and we need to extend those subviews into main views in the Laravel blade template.
If we want a more clear arrangement of pages under views then we can create folders there like ‘news’, ‘pages’ etc. let’s guess about creating a pages folder we created a view there called contact.blade.php
Now to call that view from the route file web.php is:
Route::get('contact', function () {
return view('pages.contact')->with('myname', 'Dilip Parmar');
});
Now this contact.blade.php may be big or messy if it is having large content so to make it easy we can create a subview in the same folder or any other and can extend that subview into the main view using directives called @extends
Now you know the modules in WordPress – some pieces of code. Correct? The same module we can call in Laravel named section. That is our next directive @section…@endsection, That is nothing but just of piece of code that we will use in the subview.
To use those sections of the main view in the subview, we use another directive called @yeild. SoYeid is like filler which is filling piece of code (that is sections) into subview pages.
So technically main view page receives all data from parameters/controllers and using those data, create sections and generate the layout based on that we use subviews.
For example, for the contact page, we created a main view page: contact.blade.php under folder pages.
Now we created a sub-view page under different folder layouts->pagelayots.balde.php under the views folder.
Now to extend that subview into contact.blade.php we use this code:
@extends('layouts.pagelayouts');
contact.blade.php has only a single line of code above and all the HTML is defined in pagelayouts.blade.php
Let’s create sections in contact.blade.php and to view those sections in subviews we will use @yeild.
Code in contact.blade.php
@extends('layouts.pagelayouts');
@section('pagetitle','Contact us anytime');
@section('pagecontent')
{{ $myname }}
@endsection
Code in pagelayouts.blade.php
<title>@yield('pagetitle')</title>
<body>
<h1 class="text-center">
@yield('pagecontent')
</h1>
</body>