Laravel Blade push and stack
Laravelās blade view compiler is second to none. Iāve used a couple of different templating engines and blade is by far my favourite.
Including Partials
The way in which we include partials of views within our main views is as follows:@include(āpartials.my-first-partialā)
It will inject that partialās content in the specified place.
Defining Sections
Within our views, we define āsectionsā with the following syntax:
@section(āsection_nameā)
The sectionās content within here
@stop
And we can define as many sections as we need for our project.
When the same section is used in multiple places within one compilation
Imagine we have master template file as such:
// layouts.main.blade.php <!doctype html> ...
... @yield(ācustom_scriptsā)
Letās suppose we have the following layout template that extends our main layout one and is including three partials. This example is a form template including its various inputs from separate partials. For my own website I have a different form for each of my post types and so I have the inputs in separate partials for easy reuse.
// partials.form.blade.php @extends(ālayouts.mainā) <form>@include(āparials.form-titleā) @include(āparials.form-contentā) @include(āparials.form-tagsā)</form>
Letās next suppose that in a couple of those partial input views you need to inject some custom scripting. This is a slightly contrived example, but it will illustrate the point.
// partials.form-content.blade.php <textarea class="content" name="content"></textarea> @section(ācustom_scriptsā) // dummy javascript as example $(ā.contentā).doSomething(); @stop
// partials.form-tags.blade.php <select class="tags" name="tags"> <option value="tagone">Tag One</option> <option value="tagtwo">Tag Two</option> <option value="tagthree">Tag Three</option> </select> @section(ācustom_scriptsā) $(ā.tagsā).doSomethingElse() @stop
Now, when the form page gets compiled, only the first occurrence of the ācustom_scriptsā section will be included.
So what if you needed to be able to define this section in chunks across partials?
Introducing Bladeās Push & Stack directives
To give this functionality, Laravel does in fact have two little-known directives called āpushā and āstackā.
They allow you to āstack upā items across partials with the āpushā directive, which can then be echoed out together with the āstackā directive.
Hereās the above form example but with āpushā and āstackā used in place of āsectionā and āyieldā.
// layouts.main.blade.php <!doctype html> ...
... @stack(ācustom_scriptsā)
// partials.form-content.blade.php <textarea class="content" name="content"></textarea> @push(ācustom_scriptsā) // dummy javascript as example $(ā.contentā).doSomething(); @endpush
// partials.form-tags.blade.php <select class="tags" name="tags"> <option value="tagone">Tag One</option> <option value="tagtwo">Tag Two</option> <option value="tagthree">Tag Three</option> </select> @push(ācustom_scriptsā) $(ā.tagsā).doSomethingElse() @endpush
This will now compile all uses of the @push(ācustom_scriptsā) and echo them out as one wherever you call @stack(ācustom_scriptsā)
When I was shown this technique by a mate at work, it blew my mind.
Have fun.