Laravel — P68: Defining Custom Directives in Blade

Extend Blade on your terms

How many times have you wished that a specific directive existed in Blade? If the answer to that question is “many,” then do I have a surprise for you. You can create your own directives in Blade.

First, you need to create a service provider using the php artisan make:provider command. This provider will register your Blade directives with Laravel.

# php artisan make:provider CustomBladeServiceProvider

   INFO  Provider [app/Providers/CustomBladeServiceProvider.php] created successfully.  

#
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

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

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Next, you need to register the provider in the config/app.php file.

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        // ...
        App\Providers\CustomBladeServiceProvider::class,

In your CustomBladeServiceProvider class, you can define your custom Blade directive using the Blade::directive method.

<?php

namespace App\Providers;

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

class CustomBladeServiceProvider extends ServiceProvider
{
    // ...

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

You can now use your custom directive in your Blade templates like this:

<h1>
    @money(2500)
</h1>

We simply need a route now to see it in action.

Route::get('/custom-directive', function () {
    return view('custom-directive.index');
});

The response that you get is: $2,500.00.

Alternate Approach

Don’t want to create a service provider? You can add the directive to your AppServiceProvider boot method. In that instance, you don’t have to modify the config/app.php file.

Laravel Series

Continue your Laravel Learning.

Laravel — P66: Conditional Styling in Blade

Embed logic directly into your HTML

Laravel – P67: HTML Attribute Directives In Blade

Part 67 of our Laravel fundamentals series unpacks Blade’s HTML attribute directives. Learn to conditionally apply classes, data attributes, ARIA states, and booleans using @class, @style, @disabled, @checked, and custom attribute bags for cleaner, more expressive markup.

Laravel — P68: Defining Custom Directives in Blade

Extend Blade on your terms

Laravel – P68: Defining Custom Directives In Blade

Part 68 of our Laravel fundamentals series shows you how to define your own Blade directives. Encapsulate repetitive template logic, inject reusable HTML snippets, register provider macros, and keep your views DRY, readable, and expressive.

 

Laravel — P69: Blade Slots Intro

Plug content into components with ease

Laravel – P69: Blade Slots

Part 69 of our Laravel fundamentals series introduces Blade slots—the elegant way to pass named fragments into reusable components. Learn default slots, named slots, and best practices for composing flexible, DRY layouts.

 

Leave a Reply