为什么我的@yield() 没有显示内容?在 Laravel PHP 模板中

问题描述

您好,我正在尝试学习 Laravel,我在模板中偶然发现了这个问题,其中使用 @yeild内容不显示,它只显示主刀片文件内容并且不显示子中的@section

下面是我的代码,我做错了什么?我该如何解决这个问题?


视图/布局/sample1_template.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">
    <title>Layout1</title>
</head>
<body>
    <h1>HEADER</h1>
    
    @yield('contentx')

    <h1>Footer</h1>
</body>
</html>

views/sample1_content.blade.PHP

@extends('layouts.sample1_template')

@section('contentx')

<h1> Content body </h1>

@endsection

在 web.PHP

Route::get('/sampleLayout',function() {
    return view('layouts.sample1_template');
});

解决方法

您的问题是您在 layouts.sample1_template 中仍在使用 Route,而您应该使用 sample1_content

因此,将您的 Route 代码更改为:

Route::get('/sampleLayout',function() {
    return view('sample1_content');
});