Tuesday, 10 May 2016

Unknown

Blade template Tutorial : Create Master Page and Child page using blade templates in laravel at easyLaravel

What is Blade Template -
Blade is template engine provided with laravel ,the beauty of blade template is that we can also write php code and syntax inside blade template files.

How to use -
To use blade template in laravel save your views files using .blade.php extension instead of normal .php extension.
Example : If view file which is located in resources/view directory ,its  name is index.php the to use blade syntax in that file rename it to - index.blade.php

To understand master page and child page working in laravel we use simple example .
Master Page is like a layout in which  we can create section as well as yeild other section

Child page are pages which extends master page and we write content that we needed to show for that page by creating section and other work is completed by master page.
Step 1-Define master page
Step 2-Define Child page by extending master page

Step 1-Define master page
We use @section directive to define section in  blade view files and @yeild directive is used  to display content of section.
We are going to create master page and child page in order for you to get an idea about how use blade enging syntax.
Lets create one blade file named as master.blade.php and insert following code
<!-- Stored in resources/views/layouts/master.blade.php -->

<html>
    <head>

    </head>
    <body>
        @section('sidebar')
            Inside this you can write html ,php code and javascript also.
        @show

        <div class="container">
            @yield('content') //content of section named 'content' will be displayed
        </div>
    </body>
</html>
Now as you can see master.blade.php file has @yeild directive which will show content of sections 'title' and 'content'.

Now we create child page named about.blade.php and extend the master page and  create section named 'content' and view it in browser and see the magic.
Create file named about.blade.php and save it in resources/views directory. and write following code.
<!-- Stored in resources/views/child.blade.php -->

@extends('layouts.master')

@section('content')
    <p>This is my body content.</p>
@endsection
Now using @extends we extend master page named master.blade.php ,@section('content') directive creates section named 'content' ,now we write any content using Html tags and end the section using @endsection.

Since now about page is ready we view it in browser by giving route in routes.php file as follows:

Route::get('about', function () {
    return view('about');
});

2.Show data passed to view using blade template :
We use compact()  php function to pass data to view ,we can pass array of data also to view .
Route::get('about', function () {
    $name="Easy Laravel";
    return view('about',compact('name'));  
});
Show data in view :
We use {{ variable name }} to display data from variable
<!-- Stored in resources/views/about.blade.php -->

@extends('layouts.master')

@section('content')
hello {{ $name }}
    <p>This is my body content.</p>
@endsection
3.Conditions and Loops:
Conditions :
<!-- Stored in resources/views/about.blade.php -->

@extends('layouts.master')

@section('content')
hello
@if ($name == 'Easy Laravel')
    Welcome 
@else
   Bye Bye
@endif
<p>This is my body content.</p> @endsection
For Loops and Iterations  
<!-- Stored in resources/views/about.blade.php -->

@extends('layouts.master')

@section('content')
hello {{ $name }}
@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach
<p>This is my body content.</p> @endsection
Tips : Practice all these blade tricks and in next tutorial we will create Todo application in laravel



Unknown

About Unknown -

Freelancer,Entrepreneur,Aesthetic bodybuilder.

    Knows :
  • Laravel Framework
  • Ionic Framework + Angular JS
  • Bootstrap
  • CSS
  • Jquery

Subscribe to this Blog via Email :