240gb ssd installed. So far so good.
-
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 @stopAnd 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(âpartials.formâ)... @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> ...
@yield(âpartials.formâ)... @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.