Laravel Blade - yield inside section
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
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**
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.
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:
- the root layout (layouts/app.blade.php)
- an extra layout (layouts/extra.blade.php)
- 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