Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel/Blade - Extend same template multiple times on same page

So there must be a simple way around this... On my site there are multiple modals, depending on the page. I've created a modal template that these can all extend. However, the last modal I include on the page ends up 'taking over' the rest of them, and so all my modals end up with the same sections from that last include. How can I make it so that each extension is unique to the file from which it extends?

Example of what's happening:

//template.blade.php
<htmls and stuff>
    @yield('section_1')
    @yield('section_2')
</htmls and stuff>

//Modal 1
@extends('template')
@section('section_1')
    Some words
@stop
@section('section_2')
    More words
@stop

//Modal 2
@extends('template')
@section('section_1')
    Rabbit
@stop
@section('section_2')
    Stew
@stop

Instead of two unique modals being loaded, I end up with two modals full of Rabbit Stew.

like image 498
coleman-benjamin Avatar asked Oct 23 '25 16:10

coleman-benjamin


2 Answers

Try using the @overwrite command instead of @endsection

Example:

@section('stuff')
     Stuff goes here...
@overwrite

Source: https://github.com/laravel/framework/issues/1058#issuecomment-17194530

like image 171
MASNathan Avatar answered Oct 25 '25 08:10

MASNathan


I personally would use includes in this instance, unless you've got markup in your sections. If it's just text you could do something like this:

//template.blade.php
<htmls and stuff>
    {{ $section1 }}
    {{ $section2 }}
</htmls and stuff>

//Modal 1
@include('template', ['section1' => 'Some words', 'section2' => 'More words'])

//Modal 2
@include('template', ['section1' => 'Rabbit', 'section2' => 'Stew'])
like image 45
RobbieP Avatar answered Oct 25 '25 09:10

RobbieP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!