Laravel Blade Templates Section Repeated / cache e

2019-06-20 07:11发布

问题:

I am looking to Bootstrap my site and as such, have put common twitter bootstrap components into blade templates.

sidebar.blade.php

@include('panel1')
@include('panel2')

panelTemplate.blade.php

<div class="panel panel-primary">   
    <div class="panel-heading">
        <div class="panel-title">
            @yield('title')
        </div>
    </div>
    <div class="panel-body">
            @yield('body')
    </div>
    <div class="panel-footer">
            @yield('footer')
    </div>
</div>

This way, every time I wish to use a panel, then I can use @extends('panelTemplate').

panel1.blade.php

@extends('panelTemplate')
@section('title')
 title panel 1
@stop

@section('body')
 body panel 1
@stop

@section('footer')
 footer panel 1
@stop

panel2.blade.php

@extends('panelTemplate')
@section('title')
 title panel 2
@stop

@section('body')
 body panel 2
@stop

@section('footer')
 footer panel 2
@stop

The problem that I am facing is that instead of showing the contents of panel1.blade.php, then the contents of panel2.blade.php as declared in sidebar.blade.php the contents of panel1.blade.php is being repeated (shown twice).

Is Blade caching the request which is why panel1 is being repeated twice? Is there a way to override this behaviour or am I using the blade templating engine in a way which it was never intended?

回答1:

You can achieve this by overwriting the sections.. try what you have already, but with these two updated sub-views:

panel1.blade.php

@extends('panelTemplate')

@section('title')
 title panel 1
@overwrite

@section('body')
 body panel 1
@overwrite

@section('footer')
 footer panel 1
@overwrite

panel2.blade.php

@extends('panelTemplate')

@section('title')
 title panel 2
@overwrite

@section('body')
 body panel 2
@overwrite

@section('footer')
 footer panel 2
@overwrite

It's tested and working here, though I'm not sure it's the intended use of @overwrite, so test thoroughly!