Laravel 5.4 Blade 引入了组件和插槽的概念 - 但我看不出它们相较于传统的 @include 带来了什么好处。据我理解,使用组件/插槽时,你需要:
在模板 component-tpl.blade.php 中:
<div class='container'>
<h1>{{$slot1}}</h1>
<h2>{{$slot2}}</h2>
</div>
使用页面模板中的插槽,您可以:
@component('component-tpl')
@slot('slot1')
The content of Slot 1
@endslot
@slot('slot2')
The content of Slot 2
@endslot
@endcomponent
那提供了哪些比旧版本更强大的功能:
@include('component-tpl',['slot1'=>'The content of Slot 1',
'slot2'=>"The content of Slot 2"])
使用完全相同的 'component-tpl.blade.php' Blade 模板,我漏掉了什么?感谢任何见解。
Chris
@component
看起来很不错。但是如果已经有一个使用了大量@include
的现有项目,那么记住哪些部分是@component
,哪些部分是@include
似乎会很混乱。 - Cato Minor