LoginSignup
1
0

More than 3 years have passed since last update.

[Blade]@includeWhenを使えば@if @include @endif と書くよりシンプルだった

Last updated at Posted at 2020-09-23

はじめに

@includeWhenというBladeディレクティブはご存知ですか? 私は知りませんでした。

@includeWhen

これを

@if($bool)
    @include('view.name')
@endif

こう書けます。

@includeWhen($bool, 'view.name')

ただ、以下のような落とし穴もあるようなので、使い所は限られそうです。

LaravelのincludeWhenはfalseでも右辺の評価が行われてしまう。

@if @include @endifと書く方が逆にシンプルかもしれない。

他の便利なディレクティブ

公式のドキュメントを読むと@includeIf@authなど、知ってると便利かもしれないディレクティブが色々載っています。
時間があるときに目を通して見ると、スマートな記述方法を見つけられるかもしれません。
Blade拡張を使えば、独自のディレクティブも定義できるようです。

参考

1
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
1
0