【问题标题】:did not get output with layout page in laravel在 laravel 中没有得到布局页面的输出
【发布时间】:2020-11-20 00:19:57
【问题描述】:

我是 laravel 框架的新手,想使用 laravel 布局母版页,并且习惯在母版页中包含子页面。这是我的页眉和页脚页,app.blade.php 是我的母版页,我使用@yield 显示数据。但这对我不起作用。我的输出显示为空白。

app.blade.php(布局母版页)


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    
             
    
        <title>Master Page</title>
    </head>
    <body>
            <div>
                    <div>
                            @yield('header')
                    </div>
    
            <div class="content">
                    @section('content')
                        <h1> Body </h1>
                    @endsection
            </div>
                          <div>
                                  @yield('footer')
                          </div>
    
            </div>
    
    </body>
    </html>

Header.blade.php


    @extends('app')
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title></title>
    
        <style>
            .header
            {
              height:100px;
              width:100%;
              background-color: aquamarine;
            }  
            </style>
    
    </head>
    <body>
        
    <div class="header">
        @section('header')
           <center>  Layout Header Master page </center>
        @show
    </div>
    
        
    </body>
    </html>

footer.blade.php


    @extends('app')
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            .footer
            {
              height:100px;
              width:100%;
              background-color: aquamarine;
              padding-top: 50px ;
            }  
            </style>
    
    </head>
    <body>
        
        <div class="footer">
            @section('footer')
              <center>   Layout Footer Master page </center>
              @show
        </div>
        
    
    </body>
    </html>

web.php(路由)


Route::get('/masterpage','studentcontroller@viewmasterpage')->name('masterpage');

studentcontroller.php

  

    public function viewmasterpage()
       {
           return view('layouts/app');
       }

【问题讨论】:

  • 学生控制器文件在哪里??
  • 我再次编辑我的问题,添加学生控制器文件。

标签: php laravel layout laravel-blade master-pages


【解决方案1】:

header.blade.php(只需使用代码删除其他)

@section('header')
   <center>  Layout Header Master page </center>
@endsection

footer.blade.php(只需使用代码删除其他)

@section('footer')
   <center>   Layout Footer Master page </center>
@endsection

app.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <title>Master Page</title>
</head>
<body>
        <div>
             <div class="header">
                 @include('header')
             </div>

        <div class="content">
             @yield('content')
        </div>
             <div class="footer">
                 @include('footer')
             </div>
        </div>
   </body>
</html>

studentcontroller.php

   public function viewmasterpage()
   {
       return view('layouts.app');
   }

【讨论】:

  • 使用include,可以删除@section
【解决方案2】:

问题: 你的刀片有一些问题。

  1. 每个打开的@section标签都需要一个关闭的@endsection标签。
  2. 标签部分应该包含您想要显示的所有内容。
  3. 你不需要添加整个&lt;html&gt; etc.你可以简单地添加必要的代码
  4. 我认为内容应该是收益,因为您可能希望在其中插入其他页面的内容..

我也认为你在混淆@include@yield 如果你想外包你的页眉和页脚,你可以简单地 @include('yourFolder/footer') 并插入代码


解决方案:

  1. @yield 更改为@include
  2. @section 更改为@yield('content')

示例:

文件名:header.blade.php

  <div class="header">
     <center>  Layout Header Master page </center>
  </div>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <title>Master Page</title>
</head>
<body>
        <div>
             @include('header')
        <div class="content">
             @yield('content')
        </div>
             @include('footer')
        </div>
   </body>
</html>

之后你可以创建一个新视图:example.blade.php

@extends('layout.app')
@section('content')
//put your content here
@endsection

【讨论】:

    【解决方案3】:

    您误解了@extends、@yield 和@section 指令。

    @extends 使用另一个刀片文件,并用它定义的@sections 填充@yield 指令。

    假设你有app.blade.php

    <html>
        <body>
             @yield('header')
    
             @yield('content')
    
             @yield('footer')
        </body>
    </html>
    

    然后你可以说landing.blade.php

    @extends('app')
    
    @section('header')
        <header>I am the header for the landing page!</header>
    @endsection
    
    @section('content')
        <div>I am the content for the landing page!</div>
    @endsection
    
    @section('footer')
        <header>I am the footer for the landing page!</footer>
    @endsection
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2016-02-24
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      相关资源
      最近更新 更多