Laravel 中的布局页面未获得输出

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


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



    <!DOCTYPE html>

    <html>

    <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>


沧海一幻觉
浏览 124回答 3
3回答

幕布斯6054654

问题:&nbsp;您的刀片存在一些问题。每个开始@section标签都需要一个结束@endsection标签。标签部分应该包含您想要在其间显示的所有内容。您不需要添加整个内容,<html> etc.只需添加必要的代码即可我认为内容应该是收益,因为您可能想在那里插入其他页面的内容。我还认为您很困惑@include,@yield&nbsp;如果您想外包页眉和页脚,您可以简单地 @include('yourFolder/footer') 并插入代码解决方案:更改@yield为@include更改@section为@yield('content')例子:文件名为:header.blade.php&nbsp; <div class="header">&nbsp; &nbsp; &nbsp;<center>&nbsp; Layout Header Master page </center>&nbsp; </div><!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>&nbsp; &nbsp; <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>&nbsp; &nbsp; <title>Master Page</title></head><body>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@include('header')&nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@yield('content')&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@include('footer')&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;</body></html>之后您可以创建一个新视图:example.blade.php@extends('layout.app')@section('content')//put your content here@endsection

幕布斯7119047

header.blade.php(只需使用代码删除其他)@section('header')&nbsp; &nbsp;<center>&nbsp; Layout Header Master page </center>@endsectionfooter.blade.php(只需使用代码删除其他)@section('footer')&nbsp; &nbsp;<center>&nbsp; &nbsp;Layout Footer Master page </center>@endsection应用程序.blade.php<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>&nbsp; &nbsp; <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>&nbsp; &nbsp; <title>Master Page</title></head><body>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="header">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@include('header')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@yield('content')&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="footer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@include('footer')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;</body></html>学生控制器.php&nbsp; &nbsp;public function viewmasterpage()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;return view('layouts.app');&nbsp; &nbsp;}

倚天杖

您误解了 @extends、@yield 和 @section 指令。@extends 使用另一个刀片文件,并用它定义的 @sections 填充 @yield 指令。说你有app.blade.php<html>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@yield('header')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@yield('content')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@yield('footer')&nbsp; &nbsp; </body></html>那么你可以说landing.blade.php@extends('app')@section('header')&nbsp; &nbsp; <header>I am the header for the landing page!</header>@endsection@section('content')&nbsp; &nbsp; <div>I am the content for the landing page!</div>@endsection@section('footer')&nbsp; &nbsp; <header>I am the footer for the landing page!</footer>@endsection
打开App,查看更多内容
随时随地看视频慕课网APP