Yahya Uddin
Yahya Uddin

Reputation: 28861

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

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?

Upvotes: 11

Views: 29248

Answers (5)

faisalsafadi
faisalsafadi

Reputation: 1

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

Upvotes: -4

Vishnu M Raveendran
Vishnu M Raveendran

Reputation: 411

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

Upvotes: 16

Arthur Tarasov
Arthur Tarasov

Reputation: 3791

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.

Upvotes: 5

seschi98
seschi98

Reputation: 61

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

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

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.

Upvotes: 1

Related Questions