Laravel Blade Section Directive Example

  1158 views   9 months ago Laravel

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.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]