Search

Laravel Blade Section Directive Example

post-title

Laravel provides many built-in directives. This directives helps in adding PHP codes and functionalities easily. One of these directives is @section and @yield directives.

In this article, I will show you how to use @section and @yield directives in Laravel blade. All web application uses common layout file which extends to all views.

In Laravel application, main Layout file contains @yield directives with name inside it. Here is the common layout view file at resources/views/layouts/app.blade.php.

<!DOCTYPE html>
<html>
    <head>
        <title>@yield('title') - Laravel</title>
    </head>
    <body>
        @yield('sidebar')
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

We have defined main layout view. Now we will extends this layout in any blade view. When we will add @section directive in blade view, It will be inserted to master view's @yield directive.

@extends('layouts.app')

@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

I hope it will help you on your web developement. Thanks for giving time in reading article.