Laravel's built-in templating engine, Blade, is incredibly powerful. If you've used it, you already know that.
But no matter how long you've been using it for, there's almost certainly aspects of it which you're yet to come across.
In this (admittedly very geeky post) we're looking at ten Blade directives that all developers should know about, and their use cases.
So...without further ado, let's dive in.
@production
@endproductionIf you only want your code to run when your APP_ENV variable is set to production, then this is the one for you - use it, for example, to stop your Google Analytics or Tag Manager code firing on your dev servers.
@env('local')
@endenvOn the flip side of the coin, if you only want your code to run when your APP_ENV variable is set to a local environment (staging, local, test...whatever you want to call it) then this is the one for you.
@includeIf('name.of.view', ['condition' => true])All developers will be used to using @include to allow them to break their templates up into smaller pieces for ease of storage and administration.
But most won't know that as well as the standard @include there's also the @includeIf directive, which can take a closure argument to evaluate whether or not to include the specified template, but better still, will not throw an error (unlike the standard @include) if the template doesn't exist.
There are also the @includeWhen and @includeUnless directives, as well as the @includeFirst which will take an array of views and display the first one that exists.
@something
@endsomethingSo...without further ado, let's dive in.
@stack('things')Stacks are useful to define locations in your code to which you can later push content using :
@push('things')
Stuff to push goes here
@endpushSo if you have your standard scripts that you include on every page, you may set those out in a view file. But by adding a 'scripts' stack immediately afterwards, if any other templates need to push, as a one off, a script to the stack, they can do so.
@once
@endonceAnd speaking of one-offs - if you only want a piece of code to render once, even if you're outputting it within a loop then wrap it in @once and @endonce - it'll be rendered once and once only.
This also applies to @push, by the way, in which case you want to be using @pushOnce and @endPushOnce to ensure that the enclosed content is only pushed to a stack once.
@emptySpeaking of loops, we won't insult your intelligence by teaching you to suck eggs with @foreach and @endforeach or @while and @endwhile.
But @empty can make your life easier - rather than have to count the number of, say, $users in a collection, then loop through them, but have a little bit of code to cater for displaying something when there are no users, you can kill two birds with one stone - @forelse($users as $user) followed by @empty and wrapped up with @endforelse will do the trick.
@continue
@breakThese are quick and easy - use them in loops to skip to the next iteration, or break the loop entirely, just as you would with pure PHP.
$loopYes, technically, this isn't a Blade directive, but remember that when you're iterating through a loop, the $loop variable is available to you to use (for example) @if($loop->first) or @if($loop->last).
@php
@endphpAnd if you simply cannot do it through Blade directives (and writing your own custom directive is always an option) then you can, of course, just fall back on pure PHP using these tags if you absolutely have to!
If you're using Blade alongside other tools like Google Maps in Laravel, we explored that integration in No heavy plotting.