Laravel Blade:你能产生一个默认的部分吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24549650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:45:34  来源:igfitidea点击:

Laravel blade: Can you yield a default partial

phplaravellaravel-blade

提问by user3238419

If I have a layout called RightSideBar.blade.phpin Laravel blade, one area yield('content')and the other yield('sidebar').

如果我有一个名为布局RightSideBar.blade.phpLaravel blade,一个地区yield('content')和其他yield('sidebar')

Is there a built in way to display a default partialif the view that is extending RightSideBardoes not have a section('sidebar')?

是否有一个内置的方式来显示default partial,如果被延长的观点RightSideBar没有section('sidebar')

I know you can pass a valueby default, just wondering if there is a way to make default a partial.

我知道你可以传递一个默认的,只是不知道是否有一种方法,使拖欠的部分。

回答by lozadaOmr

Yes you can pass a default

是的,您可以传递默认值

Looking at the documentation

查看文档

@yield('sidebar', 'Default Content');

@yield('sidebar', 'Default Content');

Which basically puts a default output when the child template does not have @section('sidebar')

当子模板没有时,它基本上会放置一个默认输出 @section('sidebar')

回答by Zoltan Laszlo Ferenci

Most of the time we want multiple line default content, we can use this syntax:

大多数时候我们想要多行默认内容,我们可以使用以下语法:

@section('section')
    Default content
@show

For example I have this in the template file:

例如我在模板文件中有这个:

@section('customlayout')
    <article class="content">
        @yield('content')
    </article>
@show

You can see the difference between @show and @stop/@endsection: the above code is equivalent to the one below:

可以看到@show和@stop/@endsection的区别:上面的代码等价于下面的一段:

@section('customlayout')
    <article class="content">
        @yield('content')
    </article>
@stop

@yield('customlayout')

In the other view files I can either set the content only:

在其他视图文件中,我只能设置内容:

@section('content')
    <p>Welcome</p>
@stop

Or I can also set a different layout:

或者我也可以设置不同的布局:

@section('content')
    <p>Welcome</p>
@stop
@section('defaultlayout')
    <div>
        @yield('content')
    </div>
@stop

The @stop is equivalent as the @endsection.

@stop 相当于@endsection。

回答by Yevgeniy Afanasyev

Although the docs specifies a default only as a string you can in fact pass a view

尽管文档仅将默认值指定为字符串,但您实际上可以传递视图

@yield('sidebar', \View::make('defaultSidebar'))

回答by Yevgeniy Afanasyev

Laravel 5.2 added a @hasSectiondirective that checks if a section is defined in a view. It's not mentioned in 5.3 or 5.4 docs for some reason.

Laravel 5.2 添加了一个@hasSection指令来检查是否在视图中定义了一个部分。由于某种原因,5.3 或 5.4 文档中没有提到它。

@hasSection('sidebar')
    @yield('sidebar')
@else
    @yield('default-sidebar')
@endif