在单个布局中加载多个部分时@yield和@section问题



我是Laravel的新手,我正在学习刀片系统。 我想要一个索引页面,其中包含在不同文件中定义的多个部分。

在本例中,我有一个index.blade.php,我想有两个部分,一个称为"面板",另一个部分在我的index.blade.php中称为"任务"。

这些部分在 panel.blade.php 和 create.panel.php 中定义和编码。 此外,所有三个文件都位于"view/todos/"目录中。 我正在编写的代码是下面的代码:

index.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
@yield('panel')
@yield('tasks')
</body>
</html>

对于面板刀片.php我有:

@extends('todos.index')
@section('panel')
<!--- Panel Content --->
@endsection

对于create.blade.php我有这样的东西:

@extends('todos.index')
@section('tasks')
<!--- Task Content --->
@endsection

最后,在web.php中,我有:

Route::get('/todos',function(){
return view('todos.index');
});

每当我尝试访问"/todos"地址时,它只会向我显示一个空白页。

我尝试了几种方法,将视图("todos.index"(更改为view("todos.panel"(或视图("todos.create"(,但它只向我显示了其中一个部分,而不是另一个部分。

如果有人仍然需要一些帮助,让我展示我是如何解决这个问题的:

我的模板

@include('common.header')
@yield('menu')
@yield('content')
@yield('another-stuff')
@include('common.footer')

扩展模板

@extends('common.template')
@section('menu')
@include('common.menu')
@endsection
@section('content')
@include('content')
@endsection
@section('another-stuff')
@include('another-stuff')
@endsection

html + php代码(菜单,内容和其他东西(,如您所见,我在自己的文件中分开,使内容管理变得容易。享受!o/

我不知道我是否很好地理解你的逻辑。但似乎你误解了@yield的工作原理。

相反,您可以使用文档的这一部分中的@include:https://laravel.com/docs/7.x/blade#including-subviews

我在想使用别名包含子部分从我上面链接的主要部分向下滚动一些东西。

告诉我它是否对你有帮助

最新更新