All Projects → bitpressio → Blade Extensions

bitpressio / Blade Extensions

Licence: mit
Laravel Blade Extension Classes for Laravel 5

Projects that are alternatives of or similar to Blade Extensions

Blade Ui Kit
A set of renderless components to utilise in your Laravel Blade views.
Stars: ✭ 763 (+461.03%)
Mutual labels:  laravel, blade
Blade Zondicons
A package to easily make use of Zondicons in your Laravel Blade views.
Stars: ✭ 40 (-70.59%)
Mutual labels:  laravel, blade
Laravel Notify
Flexible Flash notifications for Laravel
Stars: ✭ 787 (+478.68%)
Mutual labels:  laravel, blade
Idea Php Laravel Plugin
Laravel Framework Plugin for PhpStorm / IntelliJ IDEA
Stars: ✭ 537 (+294.85%)
Mutual labels:  laravel, blade
Laravel Tinymce Simple Imageupload
Simple image upload for TinyMCE in Laravel.
Stars: ✭ 66 (-51.47%)
Mutual labels:  laravel, blade
Blade
🔪 A standalone version of Laravel's Blade templating engine for use outside of Laravel.
Stars: ✭ 542 (+298.53%)
Mutual labels:  laravel, blade
Beautymail
Send beautiful HTML emails with Laravel
Stars: ✭ 923 (+578.68%)
Mutual labels:  laravel, blade
Coreui Free Laravel Admin Template
CoreUI Free Laravel Bootstrap Admin Template
Stars: ✭ 353 (+159.56%)
Mutual labels:  laravel, blade
Laravel Generator
laravel-generator / laravel代码生成器
Stars: ✭ 61 (-55.15%)
Mutual labels:  laravel, blade
Larawind
Larawind - Laravel 8.0+ Jetstream and Tailwind CSS Admin Theme
Stars: ✭ 55 (-59.56%)
Mutual labels:  laravel, blade
Laravel Blade X
Use custom HTML components in your Blade views
Stars: ✭ 538 (+295.59%)
Mutual labels:  laravel, blade
Laravel Blade Snippets Vscode
Laravel blade snippets and syntax highlight support for Visual Studio Code
Stars: ✭ 80 (-41.18%)
Mutual labels:  laravel, blade
Laravel Blade Javascript
A Blade directive to export variables to JavaScript
Stars: ✭ 485 (+256.62%)
Mutual labels:  laravel, blade
Artisan View
👀 Manage your views in Laravel projects through artisan
Stars: ✭ 708 (+420.59%)
Mutual labels:  laravel, blade
Comments
Native comments for your Laravel application.
Stars: ✭ 390 (+186.76%)
Mutual labels:  laravel, blade
Laravel Blade Directives
A collection of nice Laravel Blade directives
Stars: ✭ 813 (+497.79%)
Mutual labels:  laravel, blade
Shopper
An eCommerce administration built with Laravel 5 for create online shop.
Stars: ✭ 205 (+50.74%)
Mutual labels:  laravel, blade
Themevel
Theme and asset management for laravel
Stars: ✭ 278 (+104.41%)
Mutual labels:  laravel, blade
Tailwindcss
A Tailwind CSS frontend preset for the Laravel Framework
Stars: ✭ 1,056 (+676.47%)
Mutual labels:  laravel, blade
Blade Icons
A package to easily make use of SVG icons in your Laravel Blade views.
Stars: ✭ 1,181 (+768.38%)
Mutual labels:  laravel, blade

Laravel Blade Extension Classes

This Laravel >=5.5 package allows you to organize your Blade extensions into classes. It provides a simple container pass that registers all your extensions with the Blade compiler using a simple tag.

Organizing Blade extensions as classes in the service container allows you to group extension functionality within one object, allowing you to inject dependencies through the service container and provide shared protected/private methods.

Installation

You can install this package via Composer using the following command:

composer require bitpress/blade-extensions

This package will automatically register the included service provider.

Usage

At a high level, the goal of this package is to make it easy and convenient to register blade extensions as classes from the service container automatically using service container tagging. Here's the gist of how it works:

  1. Create a new Extension class with php artisan make:blade Example
  2. Register the Extension in a service provider's register() method
  3. Tag the service with blade.extension
  4. The BladeExtensionServiceProvider automatically wires up the directives in the blade compiler during boot().

Creating a new Extension Class

It would be annoying to create a new extension class from scratch each time, so this package provides an artisan command to create your extensions under the App\Blade namespace:

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

Defining the Extension Directives and Conditionals

Once you create a blade extension class, you can define supported directives, conditionals, or both. Custom Blade directives and conditionals are simply PHP callables:

<?php

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

Note that Blade extension classes implement the BladeExtension contract, which includes getDirectives() and getConditionals(). Even if you don't plan on registering any conditionals, for example, you must implement the getConditionals() method and return an empty array.

Your custom blade extension will be registered in the service container, so you can define a __construct() method and inject services from the container. The fact that Blade extensions are services allows you to group common code around your blade extensions, including those directives that depend on outside services that are registered in the container.

Registering a New Blade Service

After you define a blade extension, you still need to register it in the service container and tag it properly so the BladeExtensionServiceProvier can define the PHP callables in the Blade compiler during boot().

This package provides a BladeRegistrar class to define the extension in the container and tagging it properly, for example, in App\Providers\AppServiceProvider:

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

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

If you need to, you can create the service in a Closure:

use \App\Blade\CartExtension::class;

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

    return new CartExtension($stuff);
});

You can also use the blade_extension() helper function to register the service if you prefer:

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

Without the Blade extension registrar, this is how you'd define an extension manually:

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

The benefit of using the BladeRegistrar class is that it takes care of defining a consistent tag across any number of service providers and removes the boilerplate.

Registering Extensions with the Blade Compiler

The way this package works is quite simple, here's how the boot() method actually gets all registered extensions and defines them with the Blade compiler:

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

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].