简体   繁体   中英

Laravel Blade view not working

I am using Laravel 5.3.28 version. I have created main view(main.blade.php) and trying to extract the details from different child views(navbar.blade.php) which is in view/pages directory but its not working.

Route file in project/routes/web.php

            Route::get('/', function () {
                return view('main');
            });

Main view in resources/views/main.blade.php(this file will receive the data from navbar.blade.php)

                        <div id="column">Test1</div>
                        <div id="container">
                            @yield('content')<!--This will get the content from navbar.blade.php-->
                        </div>

child view in resources/views/pages/navbar.blade.php

            @extends('main')
            @section('content')
                    <div id="row">
                       Test2
                     </div>
            @endsection

I am able to see output as Test1 but not Test2. Why it this happening?

To see navbar content you should return navbar view:

Route::get('navbar', function () {
    return view('navbar');
});

But if you're planning to include navbar in many views, you should use @include() instead of using section() , extends() and yield() :

@include(`navbar`)

For seeing the output Test2 you have to return navbar view

Route::get('/', function () {
        return view('navbar');
    });

@yield is a place when you will extends main view then you can put something inside this content.

I am explaining below by an example using your view file

you can simply put this in navbar.blade.php. it doesn't needs to be extended.

 <div id="row">
     Test2
 </div>

In your main.blade.php you can put this

  <div id="column">Test1</div>
                    <div id="container">
                        @include('navbar')
                      @yield('content')  
  </div>

The suppose you have decided to create a third view which will extends main view with navbar view. suppose your created a view named home.blade.php and add this to your home.blade.php view

@extends('main')
@section('content')
 Welcome Home !!!
@endsection

Then if you calls create your route like below

Route::get('/home', function () {
    return view('home');
});

It will render like below

<div id="column">Test1</div>
                <div id="container">
                      <div id="row">
                        Test2
                      </div>
                 Welcome Home !!!  
               </div>

Hope this will clear your concept about view rendering

you should call view('navbar') instead of view('main') because navbar is a child of main view

if you call parent child content will not be seen there for call view('navbar')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM