Laravel <title> 在 <body>

Laravel <title> in <body>

我混合了Laravel tutorial中的两个例子,得到了结果,希望你能帮助我理解。 我的路线文件是:

Route::get('/', function () {
return view('child', ['name' => 'Samantha']);

child.blade.php 是

@extends('layouts.master')

@section('title', 'Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection
Hello, {{ $name }}.
@section('content')
    <p>This is my body content.</p>
@endsection

而master.blade.php是

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>

        @section('sidebar')
            This is the master sidebar.
        @show

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

输出为

Hello, Samantha. This is the master sidebar. This is appended to the master sidebar. This is my body content.

页面源代码为

Hello, Samantha.

<html>
    <head>
        <title>App Name - Page Title</title>
    </head>
    <body>

                        This is the master sidebar.


    <p>This is appended to the master sidebar.</p>

        <div class="container">
                <p>This is my body content.</p>
        </div>
    </body>
</html>

检查工具显示 <body> 中的 <head><title> 为空。

为什么 Hello, {{ $name }} 会出现这种情况,为什么检查工具在页面正文方面对我撒谎?

而且,如果我把@section Hello, {{ $name }} 放在里面就好了。

由于您的 child 模板扩展了布局,其中的所有数据都必须包含在一个部分中

Hello, {{ $name }}. 不在一个部分中,因此它将打印在输出的顶部。

您可以看到像缓冲区一样扩展模板。它开始包含您的 child 因为它是您在控制器中提供的文件,然后将其内容包装在布局中定义的部分中。