How to Create Custom Blade Directive in Laravel 8

Reading Time: 4 minutes
6,646 Views

Inside this article we will see concept of custom blade directive in laravel 8. If you are looking to create custom blade directive in laravel 8, this tutorial will help you.

Laravel Blade is a templating engine that compiles its special syntax back into PHP and HTML. Its special syntax includes directives. Directives are sugar-added functions hiding complex or ugly code behind them.

Few laravel directives are @if, @foreach, @csrf ,etc

Tutorial concept is super easy to understand and very simple to implement in your laravel application. With this article you will be able to create your own directives for blade template engine as @customName.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Register Custom Directive

Open AppServiceProvider.php from /app/Providers folder.

To create custom blade directives we will use Blade facade.

Step #1: Import Facade

Add this to header

use Illuminate\Support\Facades\Blade;

Step #2: Register Directive

Create custom directive inside boot() method.

Blade::directive('directiveName', callbackFunction);

Here, we have the complete code for AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Directive to find cube area
        // Formula Surface area = 6 * side * side
        Blade::directive('cubearea', function ($side) {
            return "<?php echo 6*$side*$side; ?>";
        });

        // Directive to find cube volume
        // Formula V = side * side * side
        Blade::directive('cubevolume', function ($side) {
            return "<?php echo $side*$side*$side; ?>";
        });
    }
}

Here,

We have created two custom blade directives as @cubearea and @cubevolume

@cubearea calculates surface area of cube whereas @cubevolume calculates volume of a cube.


Usage of Custom Blade Directive

Let’s see how to use created custom directives inside blade files.

Create cube-maths.blade.php file inside /resources/views folder. Write this complete code into it.

<h2>Cube Surface Area</h2>
@cubearea($side)

<h2>Cube Volume</h2>
@cubevolume($side)

Create Route

Open web.php file from /routes folder. Add this route into it.

//...

Route::get('cube', function () {
  
    $side = 10;
    
    return view("cube-maths", [
        "side" => $side
    ]);
});

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL – http://127.0.0.1:8000/cube

We hope this article helped you to learn How to Create Custom Blade Directive in Laravel 8 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.