Php 如果@yield为空,则显示视图而不是文本
对于blade,如果@yield没有数据,则可以显示默认值,如下所示:Php 如果@yield为空,则显示视图而不是文本,php,laravel,Php,Laravel,对于blade,如果@yield没有数据,则可以显示默认值,如下所示: @yield('section', 'Default Content'); 但是我真的想显示一个视图而不是字符串文本。原因是我想@在布局中产生一个导航菜单。大多数情况下,导航菜单都是完全相同的,因此某种类型的默认选项在这里应该是最佳的,但我想知道一种覆盖菜单的方法 有可能做到这一点吗?要检查该节是否存在,您可以使用: array_key_exists('fooSection', View::getSections())
@yield('section', 'Default Content');
但是我真的想显示一个视图而不是字符串文本。原因是我想@在布局中产生一个导航菜单。大多数情况下,导航菜单都是完全相同的,因此某种类型的默认选项在这里应该是最佳的,但我想知道一种覆盖菜单的方法
有可能做到这一点吗?要检查该节是否存在,您可以使用:
array_key_exists('fooSection', View::getSections())
empty(View::getSections()['fooSection'])
要检查该部分是否为空,您可以使用:
array_key_exists('fooSection', View::getSections())
empty(View::getSections()['fooSection'])
例如:
@section('fooSection')
Foo
@stop
@section('barSection')
Bar
@stop
<h1>If Section Exists</h1>
@if (array_key_exists('fooSection', View::getSections()))
@yield('fooSection')
@else
@yield('barSection')
@endif
<h1>If Section isn't Empty</h1>
@if (!empty(View::getSections()['fooSection']))
@yield('fooSection')
@else
@yield('barSection')
@endif
@section('fooSection'))
福
@停止
@节(“条节”)
酒吧
@停止
如果存在节
@如果(数组\键\存在('fooSection',View::getSections()))
@收益率('foodsection')
@否则
@产量('barSection')
@恩迪夫
如果节不是空的
@如果(!empty(View::getSections()['fooSection']))
@收益率('foodsection')
@否则
@产量('barSection')
@恩迪夫
希望这有帮助。谢谢您的回复!与此同时,我发现:@yield('navigation',View::make('layout.include.navigation'))
,尽管像@yield('navigation')、'Controller@function)
这会让变量之类的东西浪费大量时间。