Hello world, welcome to my website. Today we will discuss how to create view templates in laravel 5. I have divided this article into the following parts.
- Introduction of blade template engine
- Create View Template
- Extend template functionality
Introduction of blade template engine
Laravel comes with the powerful blade template engine. All the template files are stored in /resources/views folder. All blade files saved with .blade.php extension. There are number of template tags which can be used in the template. for example, @foreach, @if, @else, @php, @include, etc.
Create View Template
In this part, we will modify the welcome template.
- Open welcome.blade.php file in /resources/views folder and replace with the following code.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Laravel Template</title> </head> <body> Welcome to this website </body> </html>
- Now refresh the page, you will see the newly created template.
Extend template functionality
In this part, we will create common layout and extend this layout for other templates.
- Create common.blade.php and add the following code into this file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Laravel Template</title> </head> <body> @yield('content') </body> </html>
- Open welcome.blade.php file and modify the code according to the following.
@extends('common') @section('content') <p>This is content</p> @endsection
@yield tag help to load content into specified variable.
@section tag will send data to @yield tag.