spatie/laravel-blade
Add Blade to any PHP project with the standalone Blade engine from Spatie. Compile and render Blade templates outside Laravel, with an easy API, caching support, and configurable view paths—ideal for small apps, packages, or custom tooling.
Installation:
composer require spatie/laravel-blade
Add the service provider to config/app.php:
Spatie\Blade\BladeServiceProvider::class,
First Use Case: Compile a Blade template to a string:
use Spatie\Blade\Facades\Blade;
$compiled = Blade::render('path.to.view', ['data' => 'value']);
Key Files:
vendor/spatie/laravel-blade/src/Blade.php (Core class)vendor/spatie/blade/src/Facades/Blade.php (Facade for easy access)config/blade.php (Configuration, if any)Standalone Blade Rendering: Use Blade outside Laravel’s request lifecycle (e.g., CLI scripts, APIs, or non-Laravel PHP apps):
$html = Blade::render('emails.welcome', ['user' => $user]);
Dynamic View Resolution: Resolve views dynamically with custom logic:
$viewPath = $user->prefersDarkMode ? 'dark.layout' : 'light.layout';
Blade::render($viewPath, ['user' => $user]);
Component Registration: Register custom Blade components globally:
Blade::component('alert', AlertComponent::class);
Use in templates:
@alert('Success!', 'type="success"')
Directives: Create custom directives for reusable logic:
Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('Y-m-d H:i'); ?>";
});
Use in templates:
@datetime($post->created_at)
Integration with Non-Laravel Apps: Use Blade to generate HTML in standalone PHP apps by injecting the Blade compiler:
$blade = new \Spatie\Blade\Blade();
$blade->addViewPath(__DIR__.'/views');
$html = $blade->render('home', ['title' => 'Hello']);
Blade::setCachePath(null)).Blade::addViewPath(__DIR__.'/resources/views');
Blade::addViewPath(__DIR__.'/vendor/package/views');
try {
$html = Blade::render('view');
} catch (\Exception $e) {
log::error($e->getMessage());
return 'Fallback HTML';
}
Missing View Paths:
View [path] not found.Blade::addViewPath().Directive Scope:
Blade::directive('foo', function () { ... });
Blade::render('view'); // Now works
Caching Quirks:
Blade::clearCompiled()) or disable caching in development.Component Autoloading:
@component tags failing with "Class not found".Blade::component('alert', function ($slot) {
return "<div class='alert'>$slot</div>";
});
Namespace Collisions:
composer.json:
"autoload": {
"psr-4": {
"App\\": "app/",
"Views\\": "resources/views/"
}
}
Blade::setDebugMode(true); // Shows raw Blade errors
$compiled = Blade::render('view');
file_put_contents('debug.blade.php', $compiled);
$paths = Blade::getViewPaths();
log::debug('Blade view paths:', $paths);
Custom Compilers:
Extend \Spatie\Blade\Blade to modify compilation logic:
class CustomBlade extends \Spatie\Blade\Blade {
public function compile($value) {
// Pre-process templates
return parent::compile($value);
}
}
Event Hooks: Listen for Blade events (if supported in future versions) or wrap calls in middleware:
Blade::render($view, $data); // Trigger custom logic before/after
Integration with Blade Directives:
Use existing directives (e.g., @stack, @push) for layout management in non-Laravel contexts.
Blade::addViewPath()) rather than a config/blade.php file.$blade = new \Spatie\Blade\Blade();
$blade->addViewPath('/new/path');
How can I help you explore Laravel packages today?