Hi Guys! This is another basic tutorial created based on Laravel PHP framework. Views are the front end files that are shown in the web browsers. In Laravel, there’s a special view type with the extension of .blade.php. Template inheritance is the main benefit of blade templates. That means, the structure of a particular blade file can be inherited to another blade file. I will show you how to do it.

Here I use the project previously created in this article.

Go into resources/views folder. You can see a view already created by Laravel by default calling welcome.blade.php. Now I’m going to create the master blade file, which means the super template which is inherited to the other files. Create a view called master.blade.php. I link Bootstrap also to style my views.


<!DOCTYPE html>
<html>
<head>
    <title>Laravel First Project</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>
<body>
    <div class="container" style="margin-top: 30px;">
        @yield('content')
    </div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
</html>

Explanation:

@yield('content')

This code line prints the content included in the other views within their content section. This content is different from a file to another. That’s why this master is called as the template. It is same as it appears end the rest is changing.

Create another view. I will name it as sample.blade.php. Look at its content.

@extends('master')
@section('content')
<h1>This is a sample view</h1>
@endsection

Now at the beginning I inherit the master blade to this sample blade. This is the code for it.

@extends('master')

Then I start section called content which was yielded in the master blade file. Do you remember? I think so…Now enter the text you want to display in sample blade view.

Display the view

A basic routing part is needed for this. I will explain it in another article. For now, find the web.php in routes folder. In the first route you meets, change the view name from welcome to sample.


Now navigate in the project folder from cmd and type php artisan serve to run your project. Then open the web browser and type localhost:8000. Please check your post number in cmd. Most probably it will be 8000. You will see the content of sample blade file, instead of welcome blade file. We changed the base route in web.php.



This is the conclusion of Laravel views and templates. I will bring you another article on Laravel routing. Just started learning Laravel and will post what I get while trying this framework. 
Keep up your work!



1 Comments