Laravel Blade - Chain/Embed multiple layouts

孤街醉人 提交于 2019-12-04 00:17:26

You forgot using @parent. Here's the example:


master.blade.php

<html>
  <head>
    {{-- Stuff --}}
  </head>
  <body>
    @yield('content')
  </body>
</html>

nav.blade.php

You need to put the nav inside section to tell master layout that this is a content. If you don't, nav will be in the top of master layout (yes, outside html).

@extends('master')

@section('content')
  <nav>
    <p>nav content</p>
  </nav>
@endsection

home.blade.php

In this example, the content section is utilizing the @parent directive to append (rather than overwriting) content to the layout's sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.

@extends('nav')

@section('content')
  {{-- You can control where @parent should be rendered --}}
  @parent

  <home>
    <p>home content</p>
  </home>

  {{-- You can put your @parent here, give it a try --}}
@endsection

breadcrumb.blade.php

@extends('nav')

@section('content')
  {{-- You can control where @parent should be rendered --}}
  @parent

  <breadcrumb>
    <p>breadcrumb content</p>
  </breadcrumb>

  {{-- You can put your @parent here, give it a try --}}
@endsection

about.blade.php

@extends('breadcrumb')

@section('content')
  {{-- You can control where @parent should be rendered --}}
  @parent

  <about>
    <p>about content</p>
  </about>

  {{-- You can put your @parent here, give it a try --}}
@endsection

Rendered:

home.blade.php

<html>
<head>
</head>
<body>
  <nav>
    <p>nav content</p>
  </nav>
  <home>
    <p>home content</p>
  </home>
</body>
</html>

about.blade.php

<html>
<head>
</head>
<body>
  <nav>
    <p>nav content</p>
  </nav>
  <breadcrumb>
    <p>breadcrumb content</p>
  </breadcrumb>
  <about>
    <p>about content</p>
  </about>
</body>
</html>

I'm not sure I get what you're after here. For instance in home.blade.php you extend "nav", which in turn extends "master", but both "master" and "nav" yield content, so the <home> content will render twice.

So, what is your expected output? I'm not sure "home" or "about" should really extend "nav" or "breadcrumb". I think of these two as sort of structural layout components, so it does make sense to me to include them in the master layout. In "nav" you can define a section to extend when your view needs a breadcrumb.

For instance:

master.blade.php

<html>
<head><!-- stuff --></head>
  <body>
    @include('nav')        
    @yield('content')
  </body>
</html>

nav.blade.php

<nav>
  <!-- nav content -->
  @yield('breadcrumb')
</nav>

home.blade.php

@extend('master')

@section('content')
  <home>
    <!-- content -->
  </home>
@endsection

about.blade.php

@extend('master')

@section('breadcrumb')
  <breadcrumb>
    <!-- breadcrumb content -->
  </breadcrumb>
@endsection

@section('content')
  <about>
    <!-- content -->
  </about>
@endsection
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!