
Laravel 5.5 next big released is scheduled on to July 2017, that is going to have more advanced features.
Laravel 5.5 is going to have new Blade directive called, `BladeIf`, it is kind of custom condition where you can check many things like if you want to check something before displaying to user ex. check user is authenticated or not, if user is authenticated then display something something like that.
You might thinking that we can do that on blade template itself but as we know we needs to use this kind things many time and instead of repeating the statements now Laravel 5.5 going to provide more custom way to defined conditions using Blade::if directive
Let’s see how can we do that using Laravel 5.5
Table of Contents
Install Laravel 5.5 dev version:
As Laravel 5.5 is not realsed yet, let’s grab that in using –dev command, you can use following command to install new Laravel 5.5 project with dev version, please do not use it for production for now until we get the official released version
$ laravel new laravel5-5-test --dev
As this is the development version of Laravel we have to generate application encryption key:
$ php artisan key:generate
Now we are ready to go to test Laravel 5.5 Blade::if directive
How to use Laravel 5.5 BladeIf directive?
To use Laravel Blade::if directive into your laravel 5.5 project, you have to add following line of code into your App ServiceProvider class under boot() method:
`app/Providers/AppServiceProvider.php`
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Blade; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Blade::if('is_subscribed', function () { return auth()->check() && auth()->user()->isSubscribed(); }); } /** * Register any application services. * * @return void */ public function register() { // } }
So if you see the above example we are simply checking that the given user is subscribed or not and now you can easily check that out into your blade template:
Here is the test on blade template using above blade if directive:
`resources/views/welcome.blade.php`
<!doctype html> <html lang="{{ app()->getLocale() }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> </head> <body> @is_subscribed <p>User is subscribed to the particular feature of the project</p> @else <p>User is not subscribed</p> @endis_subscribed </body> </html>
So if you run the above project now you should see the message User is not subscribed as we don’t have any user or any subscription because this is the just a demo application to get you understand how you can implement Laravel 5.5 Blade::if feature into your project.
BladeIf Directive Using Parameter:
Yes, blade if directive can have parameters as well, so let’s take example of using laravel blade if directive along with the parameters:
In the following example we are going to have a blade if directive to checkout the environment and depending on the running environment (local or production) we would be adding the scripts into our blade, we often needs to do that for example we may have a script which needs to be only run on when the project is on production server, so let’s take a simple example of it:
Create new Laravel Blade if Directive:
`app/Providers/AppServiceProvider.php`
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Blade; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Blade::if('environment', function ($environment) { return app()->environment($environment); }); } /** * Register any application services. * * @return void */ public function register() { // } }
Use newly created blade if directive on blade template:
`resources/views/welcome.blade.php`
<!doctype html> <html lang="{{ app()->getLocale() }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> </head> <body> @environment('production') <script src="js/test-script.js"></script> <script> {{-- Something to only run on production --}} </script> @endenvironment </body> </html>
You can also checkout more stuff on – Laravel
If you any question related to this tutorial, let me know using comment box below.