David Peach David Peach

Web developer writing about the IndieWeb.

David Peach

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,

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(‘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.

100 Days : Day Eight (93)

Playing through The Last of Us again. I think that this is the first game since final fantasy 7 where I’ve started it again as soon as I completed it. This time I felt more bad ass because I know the enemy patterns and what to expect when. Listened to Foxes’ new album today. Is

Playing through The Last of Us again. I think that this is the first game since final fantasy 7 where I’ve started it again as soon as I completed it. This time I felt more bad ass because I know the enemy patterns and what to expect when.

Listened to Foxes’ new album today. Is pretty good on first and second listen, which reminds me I still need to write up my favourite albums of 2015.

Tickets have been booked for me, Aaron and Dwayne to go see Chas and Dave in April.

100 Days : Day Seven (94)

Decided to Install Windows 7 back on to my P.C. as the primary operating system, then run GNU/Linux within virtualbox. Thought I’d give steam games on windows another go as I’ve got 8gb of ram sat there not really doing much. This way all my development stuff can be encapsulated within the virtual machine. Also

Decided to Install Windows 7 back on to my P.C. as the primary operating system, then run GNU/Linux within virtualbox. Thought I’d give steam games on windows another go as I’ve got 8gb of ram sat there not really doing much. This way all my development stuff can be encapsulated within the virtual machine.

Also looks like I could be going to see Chas & Dave in a couple of months. I’d never heard much of their music until I started working at PM Connect. I gotta say they are growing on me.

100 Days : Day Six (95)

Missed yesterday’s entry by 50 minutes — been sat in the bath listening to this week’s episode of The Infinite Monkey Cage. Finished The Last Of Us: Left Behind. Another great addition to the world and characters from the original game. Although the relationship between Ellie and Riley was doomed, it was still nice to

Missed yesterday’s entry by 50 minutes — been sat in the bath listening to this week’s episode of The Infinite Monkey Cage.

Finished The Last Of Us: Left Behind. Another great addition to the world and characters from the original game. Although the relationship between Ellie and Riley was doomed, it was still nice to see their friendship. It gave more depth to Ellie’s character too.

Cant wait to get on to Metal Gear Solid 5 next. Just need to power through 1-4 first. I don’t have to, but I’d like to experience the whole story.

100 Days : Day Five (96)

I think I may be addicted to playing Bitcoin Billionaire. It’s annoying because I know it’s a waste of time and yet I still find myself tapping away like a mad person. Discovered a new album through some random listens today — Sex Dreams and Denim Jeans by Uffie. From the first song on this

I think I may be addicted to playing Bitcoin Billionaire. It’s annoying because I know it’s a waste of time and yet I still find myself tapping away like a mad person.

Discovered a new album through some random listens today — Sex Dreams and Denim Jeans by Uffie. From the first song on this album I was hooked.

Tinkered with my site again tonight. I think I’m gonna add in a playlists post type too.

Think I’m gonna now have an hour or so on The Last Of Us: Left Behind. So far I’m loving it.

100 Days : Day Four (97)

Slept in till 10:40. In the early afternoon we went for a run around Kingsbury Water Park. A load of the water’s-edge trees have been cut down, not sure why though. When we got there it was nice to see that the car park was pretty empty – no masses of people to avoid when

Slept in till 10:40.

In the early afternoon we went for a run around Kingsbury Water Park. A load of the water’s-edge trees have been cut down, not sure why though. When we got there it was nice to see that the car park was pretty empty – no masses of people to avoid when doing our laps.

Later on, after getting caught up with the new X Files series, Lizzie cleaned out and changed Moomin while I washed up and Cooked the tea.

I’m quite impressed with myself in keeping up this 100 days so far.