如果我在RightSideBar.blade.php
有一个名为Laravel blade
的布局,一个区域是yield('content')
,另一个是yield('sidebar')
。
如果扩展default partial
的视图没有RightSideBar
,是否有内置的方式来显示section('sidebar')
?
我知道你可以默认传递一个值,只是想知道是否有办法让默认值成为局部值。
是的,您可以传递默认值
@yield('sidebar', 'Default Content');
当子模板没有@section('sidebar')
时,它基本上放置默认输出
大多数时候我们想要多行默认内容,我们可以使用以下语法:
@section('section')
Default content
@show
例如,我在模板文件中有这个:
@section('customlayout')
<article class="content">
@yield('content')
</article>
@show
你可以看到@show和@ stop / @ endsection之间的区别:上面的代码相当于下面的代码:
@section('customlayout')
<article class="content">
@yield('content')
</article>
@stop
@yield('customlayout')
在其他视图文件中,我可以只设置内容:
@section('content')
<p>Welcome</p>
@stop
或者我也可以设置不同的布局:
@section('content')
<p>Welcome</p>
@stop
@section('defaultlayout')
<div>
@yield('content')
</div>
@stop
@stop等同于@endsection。
尽管文档仅将默认值指定为字符串,但实际上您可以传递视图
@yield('sidebar', \View::make('defaultSidebar'))
Laravel 5.2添加了一个@hasSection
指令,用于检查视图中是否定义了一个部分。由于某种原因,在5.3或5.4文档中没有提到它。
@hasSection('sidebar')
@yield('sidebar')
@else
@yield('default-sidebar')
@endif