Tuesday, 29 September 2015

Basic Usage of Views in the Laravel

We can keep views in the resources/views directory. In the laravel Html code are in the views files . It separate the controller/application logic from your presentation logic.
A  view example is given below:
<!-- View stored in resources/views/greeting.php -->

<html>
    <body>
        <h1>Hello John, <?php echo $name; ?></h1>
    </body>
</html>
This view is stored at resources/views/green.php, we may return it using the global viewhelper function like so:
Route::get('/', function ()    {
    return view('green', ['name' => 'James']);
});


The above example shows that, the first argu. passed to the view helper  in respect of name of the view file in the resources/views directory. The second argu. passed to helper is an array of data that should be made available to the view. In the above example, we are just pass the name variable, which is displayed in the view by simply executing echo on the variable.
Of course, views may also be nested within sub-directories of the resources/views directory. "Dot" notation may be used to reference nested views. For example, if your view is stored atresources/views/admin/profile.php, you may reference it like so:
return view('admin.profile', $data);


Tuesday, 22 September 2015

Extending layout in laravel

Extending A Layout

When defining a child page, you may use the Blade @extends directive to specify which layout the child page should "inherit". Views which @extends a Blade layout may inject content into the layout's sections using @section directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield:
<!-- Stored in resources/views/child.blade.php -->

@extends('layouts.master')

@section('title', 'Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection
In this example, the sidebar section is utilizing the @parent directive to append (rather than overwriting) content to the layout's sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.
Of course, just like plain PHP views, Blade views may be returned from routes using the globalview helper function:
Route::get('blade', function () {
    return view('child');
});

Monday, 21 September 2015

Best Php Framework Laravel: How to define the layout of laravel

The two way of defining the blade are template inheritance and section. Both are good to use. I like the section uses. I am just giving some example for that. It is basically good to know the uses for that. Almost all page of web application used the same layout . that is called innner layout and home layout. It is better to define those kind of layout at your web apps. giving you the single blade view.

<!-- Stored in resources/views/layouts/innermaster.blade.php -->

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>
As you can see on the above example there is HTML mark  up. However, remember the @section and @yield directives, @section is for section of  content and @yield directives is for contents of given section