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

22,165

Solution 1

Use multiple files. for eg;

layout.blade.php:

@include('header')

@yield('layout_content')

@include('footer')

second.blade.php

@extends('layout')

@section('layout_content')

<div>
@yield('second_content')
</div>

@stop

third.blade.php

@extends('second.blade.php')

@section('second_content')

<h1>Hello World!</h1>

@stop

You can include @yield in any of the parent files and can be used in the child

Solution 2

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.

Solution 3

I think it's not possible to do what you want here, at least without extending Blade: https://laravel.com/docs/5.1/blade#extending-blade

If I were you, I'd rearchitectured my views hierarchy to keep things simple.

Solution 4

I do not know if this will work, but try @include instead of @extends for your bar template. The put your section for bar below the other section (not nested). I did not tested this, so I hope it works ;)

// EDIT:

Try it with an if-statement in your foo file:

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

    @if(isset($displayBar) && $displayBar == true)
        @include('dashboard.test.bar')
    @endif
    </body>
</html>

And now the child view:

@extends('dashboard.test.foo')
@section('content')
    <p>Hello World</p>
@endsection

<?php $displayBar = true ?>

@section('bar-content')
    <p>This is in div.bar-content</p>
@endsection
Share:
22,165
Yahya Uddin
Author by

Yahya Uddin

CTO &amp; Leader Developer of The Dealer App, with a Computer Science degree from University of Warwick (UK).

Updated on July 09, 2022

Comments

  • Yahya Uddin
    Yahya Uddin almost 2 years

    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?

  • seschi98
    seschi98 about 8 years
    What does it give you as output? Is there any error?
  • Yahya Uddin
    Yahya Uddin about 8 years
    No error. Everything appears except the: <p>This is in div.bar-content</p> portion of the code
  • seschi98
    seschi98 about 8 years
    Ok, I believe it is not as easy as I wish it was, but you could put the @include into your foo file below @yield and then it would work. If you dont want to display the bar template in every child view, put an if statement with a flag onto it...
  • Andrew
    Andrew about 5 years
    While what you have written may answer the question, however it does seem a little lacking in explanation and may illicit confusion to other users. Can you please expand upon your answer so that it is clearer and more accessible? This will make for better answers and help future users understand how the problem was solved.