search

Home  >  Q&A  >  body text

I want to include a header blade file in my template, but I keep getting an "undefined variable $slot" error.

<p>I'm currently running into a problem using Laravel's Blade template. I have successfully created a template file and expanded it in different pages. However, when I try to include the header and footer in the child Blade template, I encounter a "Variable $slot is not defined" error message. <br /><br />I have used @yield() to define each section in the template file. In the specific Blade template, I use @section() to fill in the content of the corresponding section. <br /><br />Here's how I tried to include the header in the child Blade template: <br /><br />Using @include('header')<br /> Tried using @component('header') @endcomponent<br /> Unfortunately, both methods resulted in the same "undefined variable $slot" error, and my header didn't show up. <br /><br />How should I correctly include the header in the child Blade template without getting the "variable $slot is not defined" error and ensure it displays as expected? I would be very grateful for any insights or solutions to this problem. Thanks! </p><p><br /></p>
P粉596161915P粉596161915480 days ago562

reply all(1)I'll reply

  • P粉546179835

    P粉5461798352023-08-07 11:27:14

    Please do the following in your main file:

    <HTML>
    <head></head>
    <body>
    @yield('section')
    </body>
    </html>

    In another file

    @extends('layouts.app')
    @section('section')
        <p>This is appended to the master sidebar.</p>
    @endsection

    You can read more at https://laravel.com/docs/10.x/blade#extending-a-layout

    reply
    0
  • Cancelreply