Laravel Blade - exit inside the section

I am trying to give a section inside another section. But this does not work, as expected, I see an empty output.

@section('3show') This is a text string @stop @section('page-content') <div id="content"> <article> @yield('3show') </article> </div> <!--#content--> @stop 

Any ideas for displaying a section inside another section?

+11
source share
6 answers

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

Let's start by saying that to get a section into another section, you must define your included section before defining a container section. So, with this clear, I solved this situation as follows:

I have a main blade template (main.blade.php) that has something like:

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

I have a second click (common.blade.php) that has the same material that you can show on many pages, and where the main content section is defined. It looks like this:

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

Finally, I got a third template (test.blade.php) that extends the main template and includes the general material that I want to show, but be careful because the order matters. It looks like this:

 @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 must return the third template.

+4
source

In my projects, I create some partial parts to have cleaner code, and I give them an example name: 3show.blade.php. To use them in a section, I just turn them on. I think this will do what you want.

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

solution 1: you can use @show instead of @stop at the end of the section then laravel will do 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 you can view and show the result, so if you divide your section twice, the result will be doubled.

solution 2:

include the call section before yiel it for example:

  **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** 
+1
source

Suppose you have two files:

-article_base.blade.php → the default data in each article.

-article_index.blade.php -> 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

0
source

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

main.blade.php

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

child.blade.php

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

I had the same problem.

You cannot use the @extends option in this case, you need to use @include.

Let's say you have:

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

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

 @extends('layouts.app') 

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

 @include('layouts.extra') 

... and don't forget to wrap the contents of the extra layout in the @section name, for example @section ('extra')

Finally, you can invoke additional layout content from your view:

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

So, in the end, your view of someview.blade.php will look like this:

 @extends('layouts.app') @include('layouts.extra') @section('content') <p>Look below, you will see my extra content...</p> @yield('extra') @endsection 
0
source

All Articles