David Peach

Page 102 of 211

Posts by David Peach

18th February 2016

Digging into laravel is fun. Just made my first little generator tool. Laravel makes everything so bloody easy!

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

17th February 2016

So Deadpool was fucking awesome. A lot of fun.

16th February 2016

Off to watch some Alan Partridge. Why? Because mid-morning matters!

“Do You Want The Truth Or Something Beautiful” by Paloma Faith is a belter of an album. Not one song to skip.

15th February 2016

Probably the best episode of The Walking Dead yet.

13th February 2016

Version controlling my vim setup, y’all. Just sayin’

12th February 2016

Like with her last album, I was completely drawn into ‘This Is Acting’ by @Sia after the first chorus of the first song.

10th February 2016

How Chas and Dave ended up on an Eminem track

http://www.theimportanceofbeingtrivial.com/how-chas-and-dave-ended-up-on-an-eminem-song.html

Six carriages squeezed into one. Fun times.

8th February 2016

Loving the first moments on CS GO (Counterstrike: Global Offensive). Bringing back memories of CS 1.6. 2 kills and only 6 deaths!

7th February 2016

Icons Unmasked

http://laughingsquid.com/icons-unmasked-a-book-featuring-amusing-illustrations-that-reveal-the-true-identities-of-pop-culture-characters/