I created a Blade Extension package that allows you to register Blade extension classes in the service container that automatically get registered with the Blade compiler. You can also easily create new Blade extension classes with the provided php artisan make:blade
command (auto-registered package commands FTW).
The concept isn’t revolutionary by any means, but I like how it organizes my project-specific blade extensions into service container classes.
Let’s say, for example, that you have some custom directives around working with a shopping cart. Here’s a quick example of how it might look using my Blade Extensions package:
1<?php 2 3namespace App\Blade; 4 5use BitPress\BladeExtension\Contracts\BladeExtension; 6 7class CartExtension implements BladeExtension 8{ 9 public function getDirectives()10 {11 return [12 'cartcount' => [$this, 'getCartCount']13 ];14 }1516 public function getConditionals()17 {18 return [19 'cartempty' => [$this, 'isCartEmpty']20 ];21 }2223 public function getCartCount()24 {25 // logic to return cart count26 }2728 public function isCartEmpty()29 {30 // logic for empty cart31 }32}
The above extension would provide the following directives in blade:
1{{-- Conditional --}}2@cartempty3 <p>The cart is empty</p>4@else5 <p>The cart is not empty</p>6@endcartempty78{{-- Directive --}}9<span class="count">@cartcount</span>
It’s nothing special—it’s just PHP callables—but I like the feel of a dedicated class that can benefit from injecting services (i.e. a cart service) and keeping these related extensions grouped in one file.
If I need to add additional directives for the shopping cart, all I need to do is update the getDirectives()
method and define the associated callable.
You might find it interesting how this package’s service provider hooks into the Blade compiler. It’s pretty simple: the boot()
method just gets all services tagged with blade.extension
and registers the directives in the compiler.
1// In the BladeExtensionServiceProvider::boot() method 2foreach ($this->app->tagged('blade.extension') as $extension) { 3 if (! $extension instanceof BladeExtension) { 4 throw new InvalidBladeExtension($extension); 5 } 6 7 foreach ($extension->getDirectives() as $name => $callable) { 8 $this->app['blade.compiler']->directive($name, $callable); 9 }1011 foreach ($extension->getConditionals() as $name => $callable) {12 $this->app['blade.compiler']->if($name, $callable);13 }14}
The Blade Extensions package makes it easy to both create and register blade extensions in the service container:
1php artisan make:blade Cart
And this is how you register it (it also tags the service) with the provided BladeRegistrar
class:
1use App\Blade\CartExtension;2use BitPress\BladeExtension\Container\BladeRegistrar;34// ...56BladeRegistrar::register(CartExtension::class, function () {7 return new CartExtension();8});
You can also use a provided helper method instead if you prefer:
1blade_extension(CartExtension::class, function () {2 return new CartExtension();3});
Essentially this is what the BladeRegistrar
does for you:
1$this->app->singleton(CartExtension::class);2$this->app->tag(CartExtension::class, 'blade.extension');
Learn More
This package is inspired the way Twig extensions get created and registered in a Symfony project.
To get started, check out the GitHub project for complete installation instructions and workflow for creating your Blade extensions in Laravel applications.
You can install the Blade Extensions package with:
1composer install bitpress/blade-extensions
Filed in:
Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.