Laravel Blade - @slot/@component 与 @include 的优势?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44212318/
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
Laravel Blade - Advantage of @slot/@component vs @include?
提问by ChrisNY
Laravel 5.4 Blade introduced the concept of components & slots - but I can't see what they add over the traditional @include. As I understand, with component/slots, you do:
Laravel 5.4 Blade 引入了组件和插槽的概念 - 但我看不出它们在传统的 @include 上添加了什么。据我了解,使用组件/插槽,您可以:
In template component-tpl.blade.php:
在模板组件-tpl.blade.php 中:
<div class='container'>
<h1>{{$slot1}}</h1>
<h2>{{$slot2}}</h2>
</div>
Using slots in page template, you do:
使用页面模板中的插槽,您可以:
@component('component-tpl')
@slot('slot1')
The content of Slot 1
@endslot
@slot('slot2')
The content of Slot 2
@endslot
@endcomponent
What functionality does that provide over the older:
与旧版本相比,它提供了哪些功能:
@include('component-tpl',['slot1'=>'The content of Slot 1',
'slot2'=>"The content of Slot 2"])
using the exact same 'component-tpl.blade.php' Blade template?
使用完全相同的“component-tpl.blade.php”刀片模板?
What am I missing? Thanks for any insights.
我错过了什么?感谢您提供任何见解。
Chris
克里斯
回答by Rick
As stated, there's no functional difference, but careful use of both can give you cleaner blade files.
如前所述,没有功能差异,但小心使用两者可以为您提供更干净的刀片文件。
If a slot could contain HTML, then using a component will give a cleaner syntax in your blade files.
如果插槽可以包含 HTML,那么使用组件将在您的刀片文件中提供更清晰的语法。
@component('test')
<strong>This text has html</strong>
@endcomponent
versus
相对
@include('test', ['slot' => '<strong>This text has HTML</strong>'])
Equally, if a component has no slots, then an include may be preferred:
同样,如果一个组件没有插槽,那么包含可能是首选:
@include('test')
versus
相对
@component('test')
@endcomponent
回答by DavidHyogo
I think I've tracked down another crucial difference. For instance, from the documentation for 5.4:
我想我已经找到了另一个重要的区别。例如,来自 5.4 的文档:
Blade's @include directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:
Blade 的 @include 指令允许您在另一个视图中包含一个 Blade 视图。父视图可用的所有变量都可用于包含的视图:
As far as I can tell, components have a different scope from a containing view and so the variables available to the parent view are not available within the component. You need to pass a variable to a component like this:
据我所知,组件与包含视图的范围不同,因此父视图可用的变量在组件中不可用。您需要像这样将变量传递给组件:
@component('alert', ['foo' => 'bar'])
@endcomponent
This discussion is related to this problem: Use variables inside the Markdown Mailables
这个讨论与这个问题有关: Use variables inside the Markdown Mailables
回答by Denis
As the documentationsays:
正如文档所说:
Components and slots provide similar benefits to sections and layouts; however, some may find the mental model of components and slots easier to understand.
组件和插槽为部分和布局提供了类似的好处;然而,有些人可能会发现组件和插槽的心智模型更容易理解。