LoginSignup
6
8

More than 5 years have passed since last update.

[Laravel] レイアウトを多重継承する

Last updated at Posted at 2018-10-08

環境

  • Laravel 5.5

やりたいこと

  • 全体に共通する部分と一部に共通する部分のそれぞれに対してレイアウト定義を行いたい
  • ページの title を「サイト名 - ジャンル名 - サブジャンル名」としたい

方法

  • レイアウトを多重継承する
  • 親レイアウト: 全体に共通する部分を記述する
  • 子レイアウト: 一部に共通する部分を記述する(ジャンル)
  • 孫セクション: 共通しない部分を記述する(サブジャンル)

実装

親レイアウト

resources/views/layouts/parent.blade.php
<!DOCTYPE html>
<html>
    <head>
        <title>サイト名 - @yield('genre')</title>
    </head>
    <body>
        <header>
            【全体に共通するヘッダーをここに書く】
        </header>
        <main>
            @yield('main')
        </main>
        <footer>
            【全体に共通するフッターをここに書く】
        </footer>
    </body>
</html>

子レイアウト

resources/views/layouts/child.blade.php
@extends('layouts.parent')

@section('title')
    ジャンル名 - @yield('subGenre')
@endsection

@section('main')
    <h1>@yield('title')</h1>
    【一部に共通する部分をここに書く】
    @yield('notCommon')
@endsection

孫セクション

resources/views/child/grandchild.blade.php
@extends('layouts.child')

@section('subGenre', 'サブジャンル名')

@section('notCommon')
    【共通しない部分をここに書く】
@endsection

6
8
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
6
8