Javatpoint Logo
Javatpoint Logo

Template Inheritance

Master Page Layout

Master page layout defines the common layout across all the web pages. All the web applications have the master page layout to define the common layout across all the web pages. The blade templating engine defines the master layout that can be extended by all the web pages. The master page layout is available in the /resources/views/layouts/ directory.

Let's understand through an example.

  • First, create the folder named as 'layout' in resources/views/ directory.
  • Now, create a new file in layout folder 'master.blade.php'.
  • We add the following code in the master.blade.php file.

master.blade.php

In the above code, we have used the @yield directive. The @yield is used to display the content. The @yield('content') displays the content of the 'content' while @yield('footer') displays the content of the footer.

Extending Master Layout

  • Now, we are going to extend the above master layout in contact.blade.php file as shown below:

Contact.blade.php

In the above code, we use the @extends directive. The '@extends' directive is used to inherit the blade layout in contact.blade.php file. The '@section('content')' defines the section of the content.

  • Now, Add the following route in web.php file.

Output

Laravel Template Inheritance

We can also add the javascript code in contact.blade.php file. Suppose I added the following code in contact.blade.php file.

In the above code, I created the alert box, which is displaying the message "Hello JavaTpoint".

Output

Laravel Template Inheritance

Let's see another example of blade template.

  • We create a new file named as "post.blade.php" file.

post.blade.php

The above code defines the section of the content in which we are displaying the values of id, password and name respectively.

  • Now, we create a controller named as 'PostController.php'.

PostController.php

In PostController.php file, we defined a new function named as show_post() which passes the data to the post.blade.php file.

  • Lastly, we define a route in web.php file.

web.php

Output

Laravel Template Inheritance

Till now, we have seen that both the post.blade.php and contact.blade.php files are extending the master layout file. This is the main advantage of a master layout that every file can extend the layout of the master file and add their own functionalities.

Use of @parent directive

The use of @parent directive is to display the content of the section defined in the master layout.

Let's understand through an example.

  • First, we create a master file.

master.blade.php

  • Now, we create the contact.blade.php in which we extend the above master.blade.php file.

In the above code, @parent directive adds the paragraph content to the footer section.

Output

Laravel Template Inheritance
Next TopicLaravel Forms




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA