composer require initphp/viewsThe Pure PHP adapter works with the core package alone. The Blade and Twig adapters need their engine, which you install separately:
composer require illuminate/view # BladeAdapter
composer require twig/twig # TwigAdapterNothing renders until an adapter is registered on the View facade. Do this
once, during bootstrap:
require 'vendor/autoload.php';
use InitPHP\Views\Facade\View;
use InitPHP\Views\Adapters\PurePHPAdapter;
View::via(new PurePHPAdapter(__DIR__ . '/views'));If you call the facade or the view() helper before registering an adapter, a
ViewException is thrown.
The global view() helper queues the view(s), attaches the data and returns
the rendered string:
echo view('dashboard', ['username' => 'admin']);views/dashboard.php:
<h1>Welcome, <?= htmlspecialchars($username) ?></h1>A list of names is rendered in order and concatenated:
echo view(['header', 'content', 'footer'], ['title' => 'Home']);$data may be an associative array or an object. For an object, its public
properties become the data:
$user = new stdClass();
$user->username = 'admin';
$user->roles = ['editor'];
echo view('profile', $user); // $username and $roles are available in the viewThe helper is optional — the facade exposes the same workflow fluently:
use InitPHP\Views\Facade\View;
echo View::setView('header', 'footer')
->setData(['title' => 'Home'])
->render();- The facade and the
view()helper - Pick an adapter: Pure PHP, Blade, Twig
- Write your own adapter