Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Explaining @section

Hello all,

I am trying to understand this code that I am taking from: https://laravel.com/docs/5.4/blade#defining-a-layout

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

 @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

Which one is the same meaning like in php?

@extends or @section? What is @section? Part of the file?

Thanks for help.

0 likes
7 replies
gustav1105's avatar

I see it like this, use extend to extend your layout, for example you have your css and scripts and csrf tokens and your header that will be pulled into every page.

Use section where you would like to import a section of code to a page,

for example.

you have your main layout layout and on all of your other pages you will extend this main layout

and then you will have a section that you can include in on this page something like a sidebar.

AddWebContribution's avatar
  1. @extends directive to specify which layout the child view should "inherit".

  2. @section directive is inject content layout from extended blade layout and display in child blade.

  3. The content of these section will be displayed in the layout using @yield directive.

  4. @parent directive will be replaced by the content of the layout when the view is rendered.

1 like
Thyrosis's avatar

You're forgetting to pass the views and sections to your calls. Also, blade files shouldn't have the tags.

insert.blade.php

  @extends('yield')

  @section('insert')

   insert this

  @endsection

yield.blade.php (obviously with some boilerplate HTML)

<html>
  <head></head>
  <body>
    <div class="container">
      @yield('insert')
    </div>
  </body>
</html>
Snapey's avatar
//controller
return view('posts');


posts.blade.php             master.blade.php


@extends('master') ------>  //content from master
                            blah
                            blah
                            @yield('section1')
@section('section1')  <-----|
blah
blah
blah
@endsection      -------->  //more content from master
                            blah
                            blah
                            @yield('section2')
@section('section2')   <----|
blah
blah
blah
@endsection      -------->  //more content from master
                            blah
                            blah
                            //end of master file

The arrows represent the flow of the output from one file to the other

Please or to participate in this conversation.