How To Create Custom Blade Directive in Laravel 10 Tutorial

Share this Article
Reading Time: 4 minutes
115 Views

Inside this article we will see the concept of How To Create Custom Blade Directive in Laravel 10 Tutorial. Article contains classified information about How to register custom blade directive in Laravel Application.

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.

Read More: How To Create Custom Helper Function in Laravel 10 Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

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);

Read More: How To Create Custom Log File in Laravel 10 Tutorial

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 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)

Read More: How To Create Custom Route File in Laravel 10 Tutorial

Add 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 10 Tutorial in a very detailed way.

Read More: How To Create Custom Artisan Command in Laravel 10

Buy Me a Coffee

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.