PHP code example of bitpress / blade-extensions

1. Go to this page and download the library: Download bitpress/blade-extensions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bitpress / blade-extensions example snippets




namespace App\Blade;

use BitPress\BladeExtension\Contracts\BladeExtension;

class CartExtension implements BladeExtension
{
    public function getDirectives()
    {
        return [
            'cartcount' => [$this, 'getCartCount']
        ];
    }

    public function getConditionals()
    {
        return [
            'cartempty' => [$this, 'isCartEmpty']
        ];
    }

    public function getCartCount()
    {
        // logic to return cart count
    }

    public function isCartEmpty()
    {
        // logic for empty cart
    }
}

use Illuminate\Support\ServiceProvider;
use BitPress\BladeExtension\Container\BladeRegistrar;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        BladeRegistrar::register(\App\Blade\CartExtension::class);
    }
}

use \App\Blade\CartExtension::class;

BladeRegistrar::register(CartExtension::class, function () {
    // Do stuff...

    return new CartExtension($stuff);
});

public function register()
{
    blade_extension(\App\Blade\CartExtension::class);
}

$this->app->singleton(\App\Blade\CartExtension::class);
$this->app->tag(\App\Blade\CartExtension::class, 'blade.extension');

foreach ($this->app->tagged('blade.extension') as $extension) {
    if (! $extension instanceof BladeExtension) {
        throw new InvalidBladeExtension($extension);
    }

    foreach ($extension->getDirectives() as $name => $callable) {
        $this->app['blade.compiler']->directive($name, $callable);
    }

    foreach ($extension->getConditionals() as $name => $callable) {
        $this->app['blade.compiler']->if($name, $callable);
    }
}

# Creates App\Blade\CartExtension
php artisan make:blade Cart