Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Create your own custom Blade directive in Laravel

Laravel Blade comes with many in-built directives such as @section, @yield, @parent, @json and several others, all of which have a certain purpose attached to them.

For instance, the @json directive can be used to encode JSON instead of directly using json_encode like so.

<script>
    var app = @json($array);

    var app = @json($array, JSON_PRETTY_PRINT);
</script>

These, as I said, are in-built directives which comes with Laravel out-of-the-box. But what if you want to create your own custom directives? How would you do that?

Custom Blade Directive

Blade gives you the ability to define your own custom directives using the directive method on the Illuminate\Support\Facades\Blade facade. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.

For, instance, if you want to create a custom directive called @convert($var) to convert the provided number to two decimal places, you can define that into the AppServiceProvider’s boot method like so.

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    // code commented for brevity

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('convert', function ($number) {
            return "<?php echo number_format($number, 2); ?>";
        });
    }
}

Now, every time you want to format a number to two decimal places in your Blade template, you just have to call it like so.

@convert($var)

And this is how you can create your own custom Blade directive!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?