Joaquin
Joaquin

Reputation: 1

When extending a layout all the @yield are rendered instead of the @sections chosen

When trying to use @yield and @section it does not work. With only extending the layout, all the layout is rendered, I can't choose with @section and @endsection what will be rendered.

This is for a Laravel project with HomeStead on my local machine

plantilla.blade.php is:

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        <div>
            @yield('sidebar')
            This is the master sidebar.
        </div>

        <div class="container">
            @yield('content')

            This is another container
        </div>
    </body>
</html>

And the contact.blade.php is:

@extends("layouts.plantilla")

@section('content')

@endsection

When opening contact.blade.php, both sections are displayed (sidebar AND content), instead of only content, which is the section I'm actually calling.

This happens also if I just leave the first line (@extends("layouts.plantilla") without calling any section, it will render all of its content

What could am I doing wrong here?

Upvotes: 0

Views: 552

Answers (1)

zlatan
zlatan

Reputation: 3951

That's because you are yielding both content and sidebar inside your div already, so the "This is the master sidebar" and "This is another container" will always show, even if you don't use that section.

You need to change your code in main layout like this:

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
       @yield('sidebar')
       @yield('content')
    </body>
</html>

So now, if you only want to display a content of your page, you do it like this:

@extends("layouts.plantilla")

   @section('content')
      <div class="container">
        This is another container
    </div>
   @endsection

Upvotes: 1

Related Questions