LoginSignup
20
18

More than 3 years have passed since last update.

Laravelのyieldで読み込んだコンテンツの存在確認

Last updated at Posted at 2015-05-03

はじめに

Laravelのbladeテンプレート内で、@yieldを記述することで子テンプレートで定義されたコンテンツを読み込むことができます。
しかし、yieldで指定したコンテンツが存在しない時に、外側のタグのみ表示されてしまいました。

上記の問題を解決するため、外側のタグごと表示しないような制御をしてみます。

かいてみる

問題のあるケース

sample.blade.php
<div class="row">
    <div class="col-md-12">
        @yield("mainvisual")
    </div>
</div>

上記の記述をした場合、mainvisualブロックが定義されていない場合に、HTML上は以下の様なタグが出力されてしまいます

sample
<div class="row">
    <div class="col-md-12">

    </div>
</div>

対応方法

ViewクラスのhasSectionメソッドでチェックできました。

sample.blade.php
@if (View::hasSection('mainvisual'))
<div class="row">
   <div class="col-md-12">
        @yield("mainvisual")
    </div>
</div>
@endif

では、また。

20
18
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
20
18