How to Create Custom Blade Directive in Laravel 8

Share this Article
Reading Time: 4 minutes
528 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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

Open project to terminal and type the command 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.

Buy Me a Coffee

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.

Find More on Laravel 8 Articles here