Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Blade - Advantage of @slot/@component vs @include?

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:

In template component-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?

What am I missing? Thanks for any insights.

Chris

like image 801
ChrisNY Avatar asked May 27 '17 02:05

ChrisNY


People also ask

What is the advantage of Laravel blade template?

The main advantage of using the blade template is that we can create the master template, which can be extended by other files.

What is the use of slot in Laravel?

They provide an easy way of defining blade components in a Vue-like tag aliases. One of the nice features of Blade components are slots. Basically, what slot allows you to do is inject dynamic content into the blade component.

What is blade component in Laravel?

Blade components are a subset of blade template that allow you to create new custom, reusable, encapsulated PHP and HTML. For example you may want to create a navbar to be used in many of your application pages. So you build your navbar as a component and can reused the component wherever you want.

Why does Laravel use the blade template engine?

Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .


2 Answers

As stated, there's no functional difference I was incorrect - see benjaminhull's answer for details on variable scoping and passing blade syntax code. The following still holds for basic usage, though.

If a slot could contain HTML, then using a component will give a cleaner syntax in your blade files.

@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 
like image 78
Rick Avatar answered Sep 19 '22 19:09

Rick


I think I've tracked down another crucial difference. For instance, from the documentation for 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:

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

like image 30
DavidHyogo Avatar answered Sep 18 '22 19:09

DavidHyogo