Laravel 4 - Extension of the section declared in the master template more than once

I have a scenario where I have a main template that defines the header section. It looks like this...

<!DOCTYPE html> <html> <head> @section('header') {{ HTML::style('css/planesaleing.css') }} {{ HTML::script('js/jquery-1.10.1.js') }} {{ HTML::script('js/search_Bar.js') }} @show </head> <body> <div class="planesaleing_page"> <header> @yield('header_bar') @yield('nav_bar') @yield('search_bar') </header> <div class="main_page"> // Some more code </div> @yield('footer') </div> </body> </html> 

As you can see, I have several child views (e.g. nav_bar and search_bar). Each of these child views has an accompanying .js file. So I would like to expand the header section in nav_bar like this ...

 @section('header') @parent {{ HTML::script('js/anotherjs.js') }} @stop 

and then again in search_bar:

 @section('header') @parent {{ HTML::script('js/yetanotherjs.js') }} @stop 

The final output of the html file is supposed to look like this:

@section ('header') {{HTML :: style ('css / planesaleing.css)}} {{HTML :: script (' js / jquery-1.10.1.js')}} {{HTML :: script ('js / search_Bar.js')}} {{HTML :: script ('js / anotherjs.js')}} {{HTML :: script ('js / yetanotherjs.js')}} @ show

However, only the first of them actually expands the title, and all the others after that are apparently ignored. Is there a way to use multiple extensions?

Any advice is greatly appreciated.

+7
php templates laravel-4 blade
source share
1 answer

First, when you expand the layout in a view, you start a section and then β€œstop”. Like this:

 @section('header') @parent {{ HTML::script('js/anotherjs.js') }} @stop 

How do you expand layouts? I'm not sure if you have several layouts that expand each other and then expand with the help of views, or you have several views, each of which extends this particular layout. Now, in a later case, one view will not affect another view.

If you are using the previous approach, some more sample code would be useful, as I think you finished writing the values ​​at some point in time.

Hope this helps.

EDIT

when you speak:

 <header> @yield('header_bar') @yield('nav_bar') @yield('search_bar') </header> 

You create placeholders with three different names. So nav_bar, search_bar are not children, but they are placeholders that can be populated from your views.

Let me put it another way. When your views expand the template, they fill in the spaces declared in the template. Thus, each of the "headers", "search_bar", etc. Represents the gaps that ONCE needs to fill. Therefore, if you have a view named index.blade.php, you can populate them once. So you can have index.blade.php as follows:

 @section('header') @parent {{ HTML::script('js/anotherjs.js') }} @stop @section('header_bar') @parent {{ HTML::script('js/yetanotherjs.js') }} @stop @section('nav_bar') @parent {{ HTML::script('js/foojs.js') }} @stop @section('search_bar') @parent {{ HTML::script('js/foojs.js') }} @stop 

Now, if we look at this, what will it be:

 <!DOCTYPE html> <html> <head> @section('header') {{ HTML::style('css/planesaleing.css') }} {{ HTML::script('js/jquery-1.10.1.js') }} {{ HTML::script('js/search_Bar.js') }}//from parent till this point {{ HTML::script('js/anotherjs.js') }} //comes from view @show </head> <body> <div class="planesaleing_page"> <header> {{ HTML::script('js/yetanotherjs.js') }} //comes in place of header_bar as nothing in parent //@yield('header_bar') {{ HTML::script('js/foojs.js') }}//comes in place of nav_bar as nothing in parent //@yield('nav_bar') {{ HTML::script('js/foojs.js') }}//comes in place for search_bar as nothing in parent //@yield('search_bar') </header> <div class="main_page"> // Some more code </div> @yield('footer') </div> </body> </html> 

That is why only the first seems to expand the master. I think you should reconsider how you are trying to expand the wizard.

What might work for you -

 @include('view.name') // includes a subview 

However, I'm not sure if sections will be cascaded from each subset or not. You can try this.

So, you will have a view called index.blade.php that looks something like this:

 @extends('master') @include('search_bar') @include('nav_bar') @include('foo_bar') 

and then you will have separate files, such as search_bar.blade.php and others, which will expand the wizard and fill the placeholders as follows:

 @extends('master') @section('header') @parent {{ HTML::script('js/anotherjs.js') }} @stop 

I believe (did not check) that this should do your work. Then it can cascade the inputs from all subzones and stack them as you want. Thus, all sub-items will fill the header section, and this should be cascaded together in the final output. Try it, and if that works, GREAT !! otherwise try a different script injection approach.

+9
source share

All Articles