这似乎是一个奇怪的问题,但我是Laravel的新手,并且在官方文档中找到正确的语法时遇到了很多麻烦。这是一个例子。我使用以下刀片语法尝试并具有条件视图:
@if (condition)
@extends 'layouts.regular'
@else
@extends 'layouts.special'
@endif
这没用。然后我去了Laravel网站上的刀片文档并阅读了Extending a Layout部分。一切似乎都很好,我花了10分钟调试我的代码,并试图找出我做错了什么。这一切似乎都是正确的,所以我然后诉诸Google并找到了this thread。
从评论中我了解到:
虽然这解决了我的问题,但是花了太长时间才找到。我仍然无法弄清楚官方文档中提到的内容。我使用官方文档错了吗?还有其他更广泛的文档吗?我一直觉得在线文档非常简短,并且通常不能解释有关语法或异常的重要细节......
@extends总是出现在刀片的第一行,它用于定义布局,例如
假设我们有master.blade.php
<html>
<head>
</head>
<body>
@yield('content')
</body>
</html>
然后我们有page.blade.php
@extends('master') // this one says this page will be placed in master.blade.php
@section('content') // find the line that says @yield('content') and replace it with...
<h1> Hello World </h1>
@stop
现在,如果您有许多布局并且想要与它共享内容,那么您可以通过编程方式远离刀片实现...以此为例
假设我们有master.blade.php
<html>
<head>
<title>Master</title<
</head>
<body>
@yield('content')
</body>
</html>
我们有master2.blade.php
<html>
<head>
<title>Master 2</title>
</head>
<body>
@yield('content')
</body>
</html>
我们有page.blade.php
@section('content')
<h1>Hello World</h1>
@stop
// note that we have no @extends()
现在在控制器中我们做这样的事情
if($cond)
{
$layout = view('master');
}
else
{
$layout = view('master2');
}
$page = view('page');
$layout->content = $page;
return $layout;
请注意,我们使用$ layout-> content,因为两个布局都在@yield('content')中产生内容