The standalone version of Laravel's Blade templating engine for use outside of Laravel.
Install using composer:
composer require lexdubyna/blade
Create a Blade instance by passing it the folder(s) where your view files are located, and a cache
folder. Render a template by calling the make
method. More information about the Blade templating
engine can be found on https://laravel.com/docs/8.x/blade.
use Lexdubyna\Blade\Blade;
$blade = new Blade('views', 'cache');
echo $blade->make('homepage', ['name' => 'John Doe'])->render();
Alternatively you can use the shorthand method render
:
echo $blade->render('homepage', ['name' => 'John Doe']);
You can also extend Blade using the directive()
function:
$blade->directive('datetime', function ($expression) {
return "<?php echo with({$expression})->format('F d, Y g:i a'); ?>";
});
Which allows you to use the following in your blade template:
Current date: @datetime($date)
The Blade instances passes all methods to the internal view factory. So methods such as exists
,
file
, share
, composer
and creator
are available as well. Check out the
original documentation for more information.
You can make use of view components with this package.
To be able to use class-based and anonymous components, you need to register them:
$blade->compiler()->components([
'alert' => App\View\Components\Alert::class, // <x-alert type="success" message="OK" />
'components.anonymous.link' => 'link' // <x-link />
])
Your class component has to extend Jenssegers\Blade\ViewComponent
and have a protected property
$template
:
namespace App\View\Components;
use Lexdubyna\Blade\ViewComponent;
class Alert extends ViewComponent
{
// all the public properties will be exposed inside the template
public string $type;
public string $message;
protected string $template = 'components.alert'; // $template is required, it's a path to a blade template file
public function __construct($type, $message)
{
$this->type = $type;
$this->message = $message;
}
}
- tests for components
- make compatible with
illuminate/view^9.0