Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Difference between @yield and @section?

Tags:

laravel

blade

From the Laravel docs, you can include 'sections' inside layouts using two methods:

<html>     <body>         @section('sidebar')             This is the master sidebar.         @show          <div class="container">             @yield('content')         </div>     </body> </html> 

Since @yield can also pass in some default content using @yield('section', 'Default Content'), is @yield just a shorthand for a @section that does not use @parent?

@section     <!-- Nothing here --> @show 

What other differences are there?

like image 948
dayuloli Avatar asked Mar 16 '15 06:03

dayuloli


People also ask

What does @yield do in Laravel?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.

What is Section in Laravel?

@section directive is inject content layout from extended blade layout and display in child blade. The content of these section will be displayed in the layout using @yield directive. @parent directive will be replaced by the content of the layout when the view is rendered.

What is @show in Laravel?

So @show is just a compacted version of @endsection and @yield directives. I hope I made everything clear. And one more thing, in laravel 7. x to append the content in the section, @parent directive is used. Follow this answer to receive notifications.

What is @include in Laravel?

@include is just like a basic PHP include, it includes a "partial" view into your view. @extends lets you "extend" a template, which defines its own sections etc. A template that you can extend will define its own sections using @yield , which you can then put your own stuff into in your view file.


2 Answers

Short Answer: Always use @yield unless you want to do something more complicated then providing a default string.


Long Answer: Both @yield and @section .. @show are used to be optionally overwritten whenever you extend the blade template. Everything you can do with @yield can also be done with @section .. @show but not the other way around. Here is what they do:

@yield('main')

  • Can be replaced by @section('main') .. @endsection
  • Can be provided with a default string but no HTML! The default string will be shown in the sub-blade-template when no @section('main') .. @endsection is provided.

@section('main') .. @show

  • Can be replaced by @section('main') .. @endsection
  • Can be provided with a default HTML code. The default HTML code will be shown in the sub-blade-template when no @section('main') is provided.
  • Can be replaced by @section('main')@parent .. @endsection and additionally shows the default HTML code.

Here some examples:test.blade.php

<!DOCTYPE html> <html>   <head>     <meta charset="utf-8">     <title>Test</title>   </head>   <body>     <h1>This is a test</h1>      @yield('mainA')     @yield('mainB', 'This is the alternative 1')     @yield('mainC', '<p>This is the alternative 2</p>')     @yield('mainD', 'This is the alternative 3')      @section('testA')     @show      @section('testB')       This is the alternative 4     @show      @section('testC')       <p>This is the alternative 5</p>     @show      @section('testD')       <p>This is the alternative 6</p>     @show     </body> </html> 

here is another file called testA.blade.php which extends the other bladed file:

@extends('test')  @section('mainD')   <div>     <p>First replacement!</p>     <hr>   </div> @endsection  @section('testC')   <div>     <p>Second replacement!</p>     <hr>   </div> @endsection  @section('testD')   @parent   <div>     <p>Additional content</p>     <hr>   </div> @endsection 

And that is the outcome:

enter image description here

like image 107
Adam Avatar answered Sep 20 '22 03:09

Adam


This line clears out the confusion: "Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the @parent directive in a section".

So, if you already have a @section defined in the master layout, it will be overriden unless you specify @parent inside the child layout's @section.

But for @yield, it always gets the section from the child layout. That means it always overrides the @yield part, even if it has a default defined as @yield('section', 'Default Content') .

I hope that clears your confusion. Let me know if you have more questions. Thanks

like image 21
Sameer Nyaupane Avatar answered Sep 18 '22 03:09

Sameer Nyaupane