为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
沧海一幻觉 2023-09-08 17:17:59
我是 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>
查看完整描述

3 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

问题: 您的刀片存在一些问题。

  1. 每个开始@section标签都需要一个结束@endsection标签。

  2. 标签部分应该包含您想要在其间显示的所有内容。

  3. 您不需要添加整个内容,<html> 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>

<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


查看完整回答
反对 回复 2023-09-08
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

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


@section('header')

   <center>  Layout Header Master page </center>

@endsection

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


@section('footer')

   <center>   Layout Footer Master page </center>

@endsection

应用程序.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 class="header">

                 @include('header')

             </div>


        <div class="content">

             @yield('content')

        </div>

             <div class="footer">

                 @include('footer')

             </div>

        </div>

   </body>

</html>

学生控制器.php


   public function viewmasterpage()

   {

       return view('layouts.app');

   }


查看完整回答
反对 回复 2023-09-08
?
倚天杖

TA贡献1828条经验 获得超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


查看完整回答
反对 回复 2023-09-08
  • 3 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信