Johny Pie
Johny Pie

Reputation: 843

Laravel Blade - yield inside section

I'm trying to yield a section inside another section. But this does not work as expected, I see blank output.

@section('3show')
        This is a text string
@stop

@section('page-content')

<div id="content">
    <article>

        @yield('3show')

    </article>
</div>
<!--#content-->


@stop

Any ideas to yield section inside another section ?

Upvotes: 18

Views: 24365

Answers (6)

Delmontee
Delmontee

Reputation: 2364

I had the same issue.

You can't use the @extends option in this case, you need to use @include .

So lets say you have:

  1. the root layout (layouts/app.blade.php)
  2. an extra layout (layouts/extra.blade.php)
  3. the actual view that you are calling (someview.blade.php)

The solution is to use add/inherit the root layout (1) to the top line of your view (3):

@extends('layouts.app')

Next, add/inherit the extra layout (2) to the second line of your view, BUT use @include not @extends:

@include('layouts.extra')

...and remember to wrap the content of your extra layout in an @section name, for example @section('extra')

Finally you can call your extra layout content from your view:

  <p>Look below, you will see my extra content...</p>
  @yield('extra')

So in summary, your someview.blade.php view would look like:

@extends('layouts.app')
@include('layouts.extra')

@section('content')
    <p>Look below, you will see my extra content...</p>
    @yield('extra')
@endsection

Upvotes: 3

solution 1: you can use @show instead of @stop at the end of section then laravel will render your @yield parts ...

like this :

@section('page-content')

<div id="content">
    <article>

              @yield('3show')

    </article>
</div>  

@show  # not @stop




@section('3show')

        This is a text string
@stop

this way render you view and show result so if you cll your section for twice then result will be shoed twice

solution 2:

insert call section before yiel it like this :

 **first call section :**

@section('3show')

        This is a text string
@stop

**then define your section : 3show**


@section('page-content')

<div id="content">
    <article>

              @yield('3show')

    </article>
</div>  

@stop  **not @show**

Upvotes: 1

tsveti_iko
tsveti_iko

Reputation: 7972

You have to both @include and @yield the child template in your main template to be able to add the child template at a specific place inside the main template (not just before or after the main template - this is done by adding @parent in the child template - but in between):

main.blade.php

@include('child')
<div>
     ...
     @yield('child')
</div>

child.blade.php

@section('child')
     <div>
          ...
     </div>
@endsection

Upvotes: 0

M.Elwan
M.Elwan

Reputation: 1944

assume you have two files:

-article_base.blade.php -> the default data in every article.

-article_index.blade.php -> the customized file.

article_base.blade.php
@extends('layout')
@section('page-content')

<div id="content">
    <article>
        ....
        @yield('3show')
        ....
    </article>
</div>
<!--#content-->
@stop

article_index.blade.php

@extends('article_base')
@section('3show')
   This is a text string
@endsection

I hope this works

Upvotes: 0

Julian Rodriguez
Julian Rodriguez

Reputation: 574

Ok, this is what I tried and I can confirm that this works, at least for Laravel 5+ (I have L5.2). This is how I suggest you to use your blade templates.

Lets start saying that to yield a section into another section you have to define your included section before container section definition. So, with that clear, I solved this situation like this:

I got a main blade (main.blade.php) template which has something like:

<section class="content">
 <!-- Your Page Content Here -->
 @yield('main-content')
</section><!-- /.content -->

I got a second blade (common.blade.php) template which has that common stuff you may want to show across many pages and where main-content section is defined. This one looks like:

@section('main-content')
  <div class="container">
    @yield('extra-content')
  </div>
@endsection

Finally I got a 3rd template (test.blade.php) which extend the main template and include the common stuff I want to show, but be careful because the order matters. This one looks like:

@extends('main')
@section('extra-content')
  <div>
    <span> This is a test! </span>
  </div>
@endsection
@include('common')

In your controller or your route (wherever you return your view), you should return the 3rd template.

Upvotes: 6

H.Kontodios
H.Kontodios

Reputation: 346

In my projects i create some partials in order to have cleaner code and i give them as an example a name : 3show.blade.php. In order to use them in a section i just include them. I think this will do what you want.

@section('content')
    @include('3show.blade.php')
@endsection

Upvotes: 5

Related Questions