How to extend multiple templates in Blade (Laravel 5)?

Viewed 24501

I have the following files:

foo.blade.php

<html>
   <head>
   </head>
   <body>
      <h1>Foo Template</h1>
      @yield('content')
   </body>
</html>

bar.blade.php

<h2>Bar Template</h2>
<div class="bar-content">
@yield('bar-content')
</div>

I want to create another file that is able to extend both the above templates. e.g something like this:

@extends('foo')
@section('content')
     <p>Hello World</p>
     @extends('bar')
     @section('bar-content')
          <p>This is in div.bar-content</p>
     @endsection
@endsection

To give:

<html>
   <head>
   </head>
   <body>
      <h1>Foo Template</h1>
      <p>Hello World</p>
      <h2>Bar Template</h2>
      <div class="bar-content">
          <p>This is in div.bar-content</p>
      </div>
   </body>
</html>

How can I do this?

5 Answers

I would recommend using components. Here is an example.

It seems to me layout/include has a weird logic when there are many of them and you start nesting them. Components are pretty straight forward. For these nested structures you are building there, components also have slots.

@php
$ext = '';
@endphp
@if (Request::segment(1)=='explications')
@php
$ext = '_explications'
@endphp
@endif
@extends('pages_templates/template_page_fluid'.$ext)
Related