0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

@slotと@componentの使い方

Posted at

はじめに

bladeファイルの中で、何回も使用するパーツがあったりしますよね。その際に@componentを使用すると、コードがすっきりする、メンテナンスが楽になるので、使用方法を備忘録として残します。

@componentの使い方

sample.blade.php
@component(components.test)

// 表示させたい内容

@endcomponent

上記の場合、例えば「View」フォルダの「Components」フォルダ内の「test.blade.php」が表示されます。

@slotの使い方

上記の例で挙げた「test.blade.php」で変数を使用したいとします。@component内で変数を使用したい場合は、@slotを用います。

sample.blade.php
@component(components.test)

@slot('name', $name)

@endcomponent
test.blade.php
<div>
    <h1>コンポーネント</h1>
    <p class="name">{{$name}}</p>
</div>

@slotで定義した変数名の前に$をつけて記載すると、変数を表示させることができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?