aura/view
Lightweight PHP view/template system implementing TemplateView and TwoStepView patterns. Uses plain PHP templates (file or closure), supports helpers and sections, and has no userland dependencies. Install via Composer as aura/view.
Installation:
composer require aura/view
No additional dependencies are required.
Basic Setup:
use Aura\View\ViewFactory;
$viewFactory = new ViewFactory();
$view = $viewFactory->newInstance();
First Use Case:
$view->getViewRegistry()->set('home', __DIR__ . '/views/home.php');
$view->setData(['title' => 'Welcome']);
echo $view->setView('home')();
ViewFactory: For instantiating the View object.View: Core class for rendering templates.ViewRegistry/LayoutRegistry: For managing templates.HelperRegistry: For registering helpers.$view->getViewRegistry()->set('template', __DIR__ . '/views/template.php');
$view->setView('template')->setData(['user' => 'John'])->setLayout('layout');
echo $view();
$view->getViewRegistry()->set('greeting', function () {
echo "Hello, {$this->user}!";
});
$view->setView('greeting')->setData(['user' => 'Jane']);
echo $view();
$view->getLayoutRegistry()->set('default', __DIR__ . '/layouts/default.php');
// In default.php:
<html>
<body><?= $this->getContent() ?></body>
</html>
$view->setView('home')->setLayout('default');
echo $view();
$view->getHelpers()->set('escape', function ($str) {
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
});
// In template.php:
echo $this->escape($this->user);
// In template.php:
$this->beginSection('sidebar');
echo "<div>Sidebar content</div>";
$this->endSection();
// In layout.php:
if ($this->hasSection('sidebar')) {
echo $this->getSection('sidebar');
}
$view->getViewRegistry()->set('_partial', __DIR__ . '/views/_partial.php');
// In template.php:
echo $this->render('_partial', ['data' => 'value']);
// In template.php:
$this->setLayout('admin_layout');
Service Provider Integration:
Bind ViewFactory and View to the Laravel container in AppServiceProvider:
public function register()
{
$this->app->singleton(\Aura\View\ViewFactory::class, function ($app) {
return new \Aura\View\ViewFactory();
});
$this->app->bind(\Aura\View\View::class, function ($app) {
return $app->make(\Aura\View\ViewFactory::class)->newInstance();
});
}
Blade-like Usage:
Override Laravel's Blade with Aura View by extending ViewServiceProvider:
public function boot()
{
$view = $this->app->make(\Aura\View\View::class);
$view->getViewRegistry()->setPaths([base_path('resources/views')]);
// Replace Blade with Aura View in routes/controllers.
}
Middleware for View Setup: Use middleware to pre-load common data/helpers:
public function handle($request, Closure $next)
{
$view = $this->app->make(\Aura\View\View::class);
$view->setData(['user' => auth()->user()]);
$view->getHelpers()->set('flash', function ($message) {
return "<div class='alert'>$message</div>";
});
return $next($request);
}
Template Caching:
Cache compiled templates (e.g., using Aura\View\TemplateLocator with a custom cache adapter):
$view->getViewRegistry()->setLocator(new \Aura\View\TemplateLocator([
new \Aura\View\FileLocator(__DIR__ . '/views'),
new \Aura\View\CacheLocator($cacheAdapter, __DIR__ . '/views'),
]));
addData() to merge data efficiently.public function testTemplateRendering()
{
$view = (new ViewFactory())->newInstance();
$view->getViewRegistry()->set('test', function () {
return $this->data['message'];
});
$view->setData(['message' => 'Hello']);
$this->assertEquals('Hello', $view->setView('test')());
}
$view->getHelpers()->set('mockHelper', $this->createMock(InvokableInterface::class));
No Built-in Escaping:
htmlspecialchars() for HTML).Aura\Html for helpers or create custom helpers:
$view->getHelpers()->set('h', function ($str) {
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
});
Closure Scope Quirks:
extract() for passed variables:
$view->getViewRegistry()->set('partial', function (array $vars) {
extract($vars); // Required!
echo $name; // Now accessible.
});
$vars parameter and use extract($vars).Layout Overrides:
Template Paths:
setPaths():
$view->getViewRegistry()->setPaths([base_path('resources/views')]);
$view->getViewRegistry()->set('home', 'home'); // Resolves to `resources/views/home.php`.
Data Overwriting:
setData() replaces all existing data. Use addData() to merge:
$view->setData(['user' => 'Alice']);
$view->addData(['role' => 'admin']); // Merges, doesn't replace.
Helper Collisions:
Sections vs. Partials:
Template Not Found:
setPaths() and check file permissions.ViewRegistry:
$view->getViewRegistry()->setDebug(true);
Helper Not Found:
How can I help you explore Laravel packages today?