LoginSignup
0
0

More than 1 year has passed since last update.

【Laravel】@sectionと@yieldの関係(初心者向け)

Posted at

はじめに

レイアウトを作成するにあたって必須の@section@yieldについて簡単に説明します。

@section@yieldの関係

@sectionの書き方には、

index.blade.php
@section('セクション名', '画面に表示する値')

と記述するパターンと、

index.blade.php
@section(親テンプレートの@yield名)
~~~画面に表示する内容~~~
@endsection

として、親(ベース)となるテンプレートに記述された@yield(名前)部分に、@section@endsectionに囲まれた部分がはめ込まれるというパターンがあります。

具体例

layouts/app.blade.php(親となるテンプレート)
<h1>@yield('title')</h1>
<div class="container">
   @yield('content')
</div>
menus/index.blade.php(子となるテンプレート)
@section('title', '一覧画面')

@section('content')
   <p>一覧画面を表示します</p>
@endsection

app.blade.php@yield('title')部分に、index.blade.php@section('title', '一覧画面')に記述した「一覧画面」が入り、同じくapp.blade.php@yield('content')部分に、index.blade.php@section('content')に記述した<p>一覧画面を表示します</p>がはめ込まれます。

おわりに

一度理解すれば簡単ですね。
ほかにも、@extends@includeもあるので、ググってみてください。

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