The recent Laravel 5.5 release comes with some usefull blade helper and directives.
@includeFirst
Blade directive (from laravel 5.5.5)Now the following code:
@if(view()->exists('custom-template')) @include('custom-template') @else @include('default-template') @endif
Can be replaced by a single line of code:
@includeFirst(['custom-template', 'default-template'])
Now Laravel 5.5, you'll find a new syntax for defining custom if Blade directives.
The syntax might something like this in your AppServiceProvider::boot()
method:
\Blade::if('is_subscribed', function () { return auth()->check() && auth()->user()->isSubscribed(); });
and in Blade
@is_subscribed User is Subscribed. @else User is not Subscribed. @endis_subscribed
Blade::If()
Directive can also has Parameter
Blade::if ('environment', function ($environment) { return app()->environment($environment); });
and in blade template
@environment('production') your code goes here...... @endenvironment
An indepth tutorial about this new featurs can be found at Laracast video Custom Blade "If" Directives.
@auth
and @guest
DirectivesThese new shortcuts permit to quickly determine if the current user is authenticated or is a guest:
@guest
guest stuff here
@else
logged user stuff here
@endauth
or
@auth logged user stuff here @endauth
or if you're using a Guard
@auth('admin') logged in through admin guard @endauth
@json
Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable. For example:
<script>
var app = json-decode($array) ;
script>
can be replaced by:
<script>
var app = @json($array);
script>
Learning More About Laravel new Blade directives @ What's New in Laravel 5.5: New Blade Directives