Skip to content

Template Component

Jorge Castro edited this page Sep 18, 2020 · 5 revisions

What is a component?

It is a reusable visual object that could be used many times and it could even call itself.

Tag Description Example
@component/@endcomponent Call a component.
Optionally, it allows to pass arguments
@component("aaa.bbb",[$a=>1,$b=2])
@endcomponent
@slot/@endslot Pass part of the code as a variable @slot("myslot")
--some content--
@endslot
$slot It is a special variable $slot

You can find the example in

https://github.com/EFTEC/BladeOne/blob/master/examples/testcomponent.php

Creating a component

📄 TestComponent/alert.blade.php

<div class="alert alert-danger" style="background-color: {!! $color  !!}; padding-left: 100px;">
    <div>{!! $title !!}</div>
    {!! $slot !!}
</div>
  • $slot is a special variable. It is used to show the content of the caller (excluding every content inside @slot/@endslot)

Calling the component using variables

📄 TestComponent/component.blade.php

You can call the component using the tag @component, and passing the variables as the argument.

@component('TestComponent.alert',array('title'=>'COMPONENT #1','color'=>"red"))
    <strong>Whoops!</strong> Something went wrong! (the code is right btw)
@endcomponent
  • $slot corresponds to the content: "Whoops! Something went wrong! (the code is right btw)"

Calling the component using the tag slot

📄 TestComponent/component.blade.php

You can also use the tag @slot to pass values to the component.

@component('TestComponent.alert',array('color'=>"red"))
    @slot('title')
    	COMPONENT #1
    @endslot
    <strong>Whoops!</strong> Something went wrong! (the code is right btw)
@endcomponent