您正在混合 Laravel 的两种不同布局方法。
通过这种方式,您可以渲染布局视图,包含主视图并尝试再次包含在布局内部。
我个人的偏好是控制器方法。
控制器布局
控制器和布局可以保持不变。
注意:作为快捷方式,您可以嵌套内容而不是 View::make,当您在布局中回显内容时,它会自动呈现它。
在 home.blade.php 中删除 @layout 函数。
编辑(示例):
控制器/home.php
<?php
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->title = 'title';
$this->layout->nest('content', 'home', array(
'data' => $some_data
));
}
}
视图/布局/default.blade.php
<html>
<title>{{ $title }}</title>
<body>
{{ $content }}
</body>
</html>
视图/home.blade.php
部分内容包含在内容中。
@include('partials.header')
{{ $data }}
@include('partials.footer')
刀片布局
如果您想要这种方法,那么您会遇到一些问题。首先,您在布局之后添加新内容。不确定是否是故意的,但@layout函数本身基本上只是一个@include限制在视图的最开始。因此,如果您的布局是封闭的 html,则其后的任何包含内容都将附加在您的 html 布局之后。
您的内容应使用此处的部分@section功能和@yield它在你的布局中。页眉和页脚可以包含在布局中@include或者,如果您想在内容视图中定义它,则将它们放在@section也是如此,如下所示。如果您以这种方式定义它,如果一个部分不存在,则不会产生任何结果。
控制器/home.php
<?php
class Home_Controller extends Base_Controller {
public function action_index()
{
return View::make('home')->with('title', 'title');
}
}
视图/布局/default.blade.php
<html>
<title>{{$title}}</title>
<body>
@yield('header')
@yield('content')
@yield('footer')
</body>
</html>
视图/home.blade.php
@layout('layouts.default')
@section('header')
header here or @include it
@endsection
@section('footer')
footer
@endsection
@section('content')
content
@endsection