Create Custom Blade Directives in Laravel
Tags: laravel, php, blade, directive
Published: 2023-08-27 21:27:34

Laravel Blade is awesome, really awesome. One of the greatest things about it, is the ability to add custom directives. 
One use case I recently stumbled upon, is formatting money, with the currency. For sure I can create a helper function and call it. 
Example 
```php
    if (! function_exists('formatMoney')) {
    function formatMoney(string|int|float $amount) {
        if (is_string($amount)) {
            $amount = floatval($amount);
        }

        $settings = app(GeneralSettings::class);

        $formatter = new NumberFormatter(config('app.currency_locale', 'en_US'), NumberFormatter::CURRENCY);

        return $formatter->formatCurrency($amount, $settings->app_currency);

    }

```
And then we could use it in our views like the following 
```blade
...
{{ formatMoney($invoice->total) }}
...
```

And we're good to go, but in this context, let's use the power of Blade and create our own directive. 
In order to do so, open `AppServiceProvider.php` and head to the `boot` method. 
Inside the boot method, we will write our own custom directive. 

```php
Blade::directive($name, function ($expression) {
    some code here;
});
```

In our context we will use the code above, defined in a helper class, so we can format the money. 
The blade directive will be named `money` 
```php
Blade::directive('money', function ($expression) {
    return "<?php echo app('App\Utils\Helpers')->formatMoney($expression); ?>";
});
```
So here is the final code, and in the view file, we can call it as follows 
```blade
@money('somevalue')
```
The take here is in the return value, if you want to have some formatters, or some conditions, the return must be a PHP code. Let's take another example. 
Laravel has `@auth` & `@guest` directives. 
But let's make some thing on our own. Let's say we have customers in our application, so we will make a blade directive that checks if the authenticated user is a customer or not, and show some code based on that condition.
Usually, here is what we would do : 
```blade
@if(auth()->user()->isCustomer)
//some code here
@endif
```
But, that would be hard if we have that piece of code everywhere in our views. Imagine if we will add another condition, like checks if the customer has an active subscription for example.
So yeah, you've got the point. 
In this situation, we could leverage blade custom directives.
The use of the new directive would be as follows : 
```blade
@customer
//somecode here
@endcustomer
```
As you can see here, we have two directives, so we will define two functions in our `AppServiceProvider.php`
```php
Blade::directive('customer', function ($expression) {
    return "<?php if(auth()->check() && auth()->user()->isCustomer): ?>";
});

Blade::directive('endcustomer', function ($expression) {
    return "<?php endif; ?>";
});

```
The first one is to test the condition and open the colon for your code logic, and the other one, will close the the if statement. 

In this short tutorial, we saw how we could write custom blade directives. I hope it was insightful for you to understand how to create and use them. 
If there is any question or any suggestion, feel free to reach me on [Twitter](https://x.com/koossaayy) 

[Original Post](https://koossaayy.tn/create-custom-blade-directives-in-laravel-)