Laravel | Blade Templates Inheritance Last Updated : 22 Nov, 2019 Comments Improve Suggest changes Like Article Like Report A templating engine makes writing frontend code easier and helps in reusing the code. All the blade files have a extension of *.blade.php. In Laravel, most of the times frontend files are stored in resources/views directory. Blade files support PHP and are compiled into plain PHP and cached in the server so that we don't have to do the extra work of compiling the templates again when a user access a page again, thus using Blade is as efficient as using PHP files itself in the frontend. Template Inheritance: In most of the modern webpages, a fixed theme is followed in all the webpages. Thus it is greatly effective to be able to reuse your code so that you don't have to write again the repeating parts in your code and Blade greatly helps you in achieving this. Defining a layout: Let's do that with an example and create a file called layout.blade.php in resources/views directory as shown below: php <!DOCTYPE html> <html lang="en"> <head> <title>@yield('title')</title> </head> <body> <div> @yield('content') </div> </body> </html> Now, in the code given above, we use @yield directive to tell the Blade that we are going to further extend this part in the child blade pages. Further, notice that each of yield directive is having a name like title for first one and content for second one. These names are going to be used later in the child page to tell that this section is extended here. Extending a layout: Let's do that too now and create a page at resources/views directory called mypage.blade.php as given below: php @extends('layout') @section('title') Child Page @endsection @section('content') <h1>My first page with Blade Inheritance.</h1> @endsection In this code, we are first using the @extends directive which tells which blade page we are inheriting this page from. In our case, it is going to be layout as we are going to inherit this page from layout.blade.php, we created earlier. Further, we use the @section directive to extend each of the @yield directive's of the parent blade file. We have to tell the name of each @yield directive we are extending here in the @section directive as we have done in code above. Make sure after writing the code you end the directive with @endsection. All the @yield sections will be replaced with the respective code in the child blade pages. One last thing left to make this work is adding a route as given below in your routes/web.php. php Route::get('/mypage', function() { return view('mypage'); }); We just created a route to /mypage and in the callback function we are serving mypage.blade.php. Notice that Blade automatically looks for files in resources/views directory. Output: In the output you can see how @yield('title') is replaced with Child Page and @yield('content') is replaced with My first page with Blade Inheritance. Comment More infoAdvertise with us Next Article Laravel | Blade Templates Inheritance G gurrrung Follow Improve Article Tags : Web Technologies PHP Laravel Similar Reads Template Inheritance in Flask Template inheritance is a powerful feature in Jinja, the templating engine used in Flask. It allows us to define a common structure for web pages, such as headers, footers, and navigation bars, in a base template. This prevents redundant code and makes managing multiple pages easier.Prerequisite - F 2 min read Template Inheritance: Extend and Block in Pug While creating websites or different web applications, we can first create a base HTML template, and build different pages on top of the base template. This concept of using building pages over a base template is called Template Inheritance. It's a powerful feature available in web template engines 2 min read Laravel | Delete Records To delete records we can use DB facade with the delete method. To do so follow the below steps one by one: Step 1: Create Controller UserController by executing this command. php artisan make:controller UserController Step 2: We can delete records in two ways. First Method: The first is to delete di 2 min read Laravel vs Spring Boot: Top Differences Laravel and Spring Boot are the two most popular tools used to develop applications. A comparison between the two can be seen as a scenario where you need to build a house. You can either use all the tools that will help build the house with wood or tools that will help build the house with metal. T 9 min read Laravel | Migration Basics In Laravel, Migration provides a way for easily sharing the schema of the database. It also makes the modification of the schema much easier. It is like creating a schema once and then sharing it many times. It gets very useful when you have multiple tables and columns as it would reduce the work ov 4 min read Like