Laravel yield 不显示内容

Laravel yield doesn't show the contents

Yield 函数不会加载其他文件中的特定部分。

路线

Route::get('/', function () {
    return view('add-listing');
});

添加-listing.blade.php

@extends('layout')

@section('body')
  This is add-listing page
@stop

header.blade.php

@extends('layout')

@section('header')
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Find Do Responsive Directory Template</title>
    <link rel="stylesheet" type="text/css" href="css/master.css">
    <link rel="stylesheet" type="text/css" href="css/color-green.css">
    <link rel="stylesheet" type="text/css" href="css/app.css">
    <link rel="shortcut icon" href="images/short_icon.png">
@stop

layout.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    @yield('header')
</head>
<body>

    @yield('body')

</body>

当我 运行 时,只有 @section('body') 内容正在加载。它不会加载 @section('header')。这是为什么?

您可以在布局中使用 @include('header') :

<!DOCTYPE html>
<html lang="en">
<head>
    @include('header')
</head>
<body>

    @yield('body')

</body>

header.blade.php :

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Find Do Responsive Directory Template</title>
    <link rel="stylesheet" type="text/css" href="css/master.css">
    <link rel="stylesheet" type="text/css" href="css/color-green.css">
    <link rel="stylesheet" type="text/css" href="css/app.css">
    <link rel="shortcut icon" href="images/short_icon.png">

在这个例子中你调用 add-listing.blade.php 而在这个文件中你只是说调用 layout.blade 所以你的刀片不调用 header.blade 只是准备一个部分用于获取 header 内容并且您不在流程中调用 header.blade。

Yield是一个可以分段设置的变量:

@yield('header') ~ echo $header

@section('header')

header 值在这里

@endsection

所以 $header = header 值在这里。