Blade is Laravel’s powerful templating engine, and it allows developers to simplify repetitive code using components and directives. This tutorial focuses on creating custom Blade components and directives to improve code reusability, readability, and maintainability in your Laravel applications.


Scenario: Custom Blade Components and Directives

Consider a scenario where:

  • You need reusable components for buttons, modals, or alerts.
  • Complex logic in Blade templates can be simplified using custom directives.
  • You want to ensure your templates follow DRY (Don’t Repeat Yourself) principles.

We’ll create custom Blade components and directives to streamline your workflow.


Step 1: Creating Custom Blade Components

  1. Generate a Blade Component:

Use the Artisan command to generate a component:


   php artisan make:component Button

This creates:

  • app/View/Components/Button.php (Logic for the component)
  • resources/views/components/button.blade.php (Template for the component)
  1. Define the Component Logic:

In app/View/Components/Button.php:


   namespace App\View\Components;

   use Illuminate\View\Component;

   class Button extends Component
   {
       public $type;
       public $label;

       public function __construct($type = 'primary', $label = 'Submit')
       {
           $this->type = $type;
           $this->label = $label;
       }

       public function render()
       {
           return view('components.button');
       }
   }
  1. Create the Blade Template:

In resources/views/components/button.blade.php:


   
  1. Use the Component in Blade Templates:

In any Blade view:


   
   

Output:

   
   

Step 2: Creating Components with Slots

Slots allow you to pass additional content into components.

  1. Add a Slot to the Component:

Modify resources/views/components/button.blade.php:


   
  1. Use the Slot in Blade Views:

   
       Click Me
   

Output:

   

Step 3: Passing Attributes Dynamically

  1. Add Dynamic Attributes:

Modify resources/views/components/button.blade.php:


   
  1. Use Dynamic Attributes:

   
       Submit
   

Output:

   

Step 4: Creating Custom Blade Directives

  1. Define a Custom Directive:

Add the directive in AppServiceProvider:


   use Illuminate\Support\Facades\Blade;

   public function boot()
   {
       Blade::directive('uppercase', function ($expression) {
           return "";
       });
   }
  1. Use the Directive in a Blade View:

   @uppercase('hello world')

Output:


   HELLO WORLD
  1. Advanced Example: Condition-Based Directive:

Define a directive for user roles:


   Blade::if('admin', function () {
       return auth()->check() && auth()->user()->isAdmin();
   });

Use it in Blade templates:


   @admin
       

Welcome, Admin!

@else

You do not have admin access.

@endadmin

Step 5: Testing Your Components and Directives

  1. Testing Blade Components:

Use Laravel’s testing tools to verify component rendering:


   public function testButtonComponent()
   {
       $view = $this->blade('');

       $view->assertSee('btn-success')
           ->assertSee('Test');
   }
  1. Testing Blade Directives:

Test the output of custom directives:


   public function testUppercaseDirective()
   {
       $view = $this->blade("@uppercase('test')");
       $view->assertSee('TEST');
   }

Key Takeaways

  • Use Blade components to encapsulate repetitive UI elements like buttons, alerts, and modals.
  • Leverage slots and dynamic attributes for flexible and reusable components.
  • Simplify complex template logic using custom Blade directives.
  • Test your components and directives to ensure they behave as expected.

Conclusion

Blade components and directives are powerful tools for improving the readability and reusability of your Laravel templates. By creating custom components and directives, you can streamline your workflow and adhere to best practices for clean and maintainable code.