Tag: Web Development

  • 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.

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

  • @letsencrypt hi. I signed up for beta access the other day for my site davidpea.ch I was just wondering if you had a timescale? Cheers.

  • Alias allthethings

  • Starting a new personal project tonight. Finally a medium-sized project idea that I want to build. And one to really dig into Laravel.

  • The amount of old repositories I’m deleting from my Github account is making me think how often I start new projects and never complete them. I really need to change that.

  • Setting up samba share on my home network

    Secure shell into file server.

    Install samba if not already present:

    sudo apt-get install samba

    Create samba password with :

    sudo smbpasswd -a YOUR_USERNAME

    Configuring the share:

    sudo nano /etc/samba/smb.conf
    # /etc/samba/smb.conf
    
    [media] 
    path = /home/YOUR_USERNAME/Share 
    available = yes 
    valid users = YOUR_USERNAME 
    read only = no 
    browsable = yes 
    public = yes 
    writable = yes

    Restart samba:

    sudo restart smbd
  • Bypassing Laravel’s CSRF Middleware on selected routes (from 5.1)

    Laravel does a great job at protecting us from cross-site request forgeries – or C.S.R.F. for short.But sometimes you may not wish to have that layer present. Well with Laravel 5.1 you can very easily bypass this middleware, simply by populating an array in the following file:

    app/Http/Middleware/VerifyCsrfToken.php

    Within this class you can add a protected property — an array — called $except, which will tell Laravel to use this middleware except for the ones you specify here.

    A complete example could be:

    protected $except = [
        'ignore/this/url',
        'this/one/too',
        'and/this',
    ];

    So for those three URLs, the CSRF middleware would be skipped.

  • Every time I do a “git add .”, I feel a little dirty inside.

  • Trying to get SMS posting to my website working this late at night. What was I thinking. Nearly there though I think.

  • So planning and building an e-commerce CMS is harder than it seemed at first. Not that I thought it would be easy.

  • 100 days 2015 – 10 days in

    For the past ten days I have been doing the 100 day challenge.

    I decided to have a go at building my own e-commerce CMS.

    I don’t have a big passion for e-commerce. In fact most of the time I work with e-commerce it frustrates me. But then I always remember hearing the old saying:

    “The best way of complaining about something is to try and build something better”.

    I won’t say which e-commerce CMS has me often cursing. Those who know me will know by the curses and moaning directed at it.

    It feels good to be 10 days in. It’s also the longest commit streak ever on my github account.

    I think the biggest thing I am taking away from my 100 day project is the learning I’m doing along the way. The outcome is kind of irrelevant at this point in time — although it would be nice to have something to show for it at the end.