Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Blade - yield inside section

Tags:

laravel

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 ?

like image 655
Johny Pie Avatar asked Aug 30 '16 16:08

Johny Pie


People also ask

What is yeild (' content ') 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 the difference between yield and section in Laravel?

@yield is a section which requires to be filled in by your view which extends the layout. You could pass it a default value through the second parameter if you'd like. Usages for @yield could be the main content on your page. @section is a section which can contain a default value which you can override or append to.

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 @stack in Laravel?

One of blade directive @stack('scripts') is very helpful when you have javascript need to execute in the child page. I create a fresh Laravel installation to demo an example. I have to make auth scaffolding with laravel/ui package because I'm using Laravel 6.


1 Answers

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.

like image 114
Julian Rodriguez Avatar answered Sep 18 '22 14:09

Julian Rodriguez