简体   繁体   English

Laravel Blade视图不起作用

[英]Laravel Blade view not working

I am using Laravel 5.3.28 version. 我正在使用Laravel 5.3.28版本。 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. 我创建了主视图(main.blade.php),并尝试从view / pages目录中的不同子视图(navbar.blade.php)中提取详细信息,但它不起作用。

Route file in project/routes/web.php 路线文件位于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) 资源/视图/main.blade.php中的主视图(此文件将从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 资源/视图/页面/navbar.blade.php中的子视图

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

I am able to see output as Test1 but not Test2. 我能够看到输出为Test1,但看不到Test2。 Why it this happening? 为什么会这样呢?

To see navbar content you should return navbar view: 要查看导航栏内容,您应该返回navbar视图:

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()而不是section()extends()yield()

@include(`navbar`)

For seeing the output Test2 you have to return navbar view 要查看输出Test2,您必须返回navbar视图

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

@yield is a place when you will extends main view then you can put something inside this content. @yield是扩展main view的地方,然后可以在此内容中添加一些内容。

I am explaining below by an example using your view file 我在下面通过使用您的视图文件的示例进行说明

you can simply put this in navbar.blade.php. 您只需将其放在navbar.blade.php中。 it doesn't needs to be extended. 它不需要扩展。

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

In your main.blade.php you can put this 在您的main.blade.php中,您可以将其

  <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 假设您创建了一个名为home.blade.php的视图,并将其添加到您的home.blade.php视图中

@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 您应该调用view('navbar')而不是view('main'),因为navbar是主视图的子级

if you call parent child content will not be seen there for call view('navbar') 如果您呼叫父级子级内容,则不会在呼叫视图中看到那里的内容(“ navbar”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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