-
-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Five minutes from composer require to a rendered page.
Nothing renders until an adapter is registered on the View
facade. Do this once, during bootstrap:
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use InitPHP\Views\Facade\View;
use InitPHP\Views\Adapters\PurePHPAdapter;
View::via(new PurePHPAdapter(__DIR__ . '/views'));via() takes a ready-made adapter instance (what you almost always want), or the
class name of an adapter that needs no constructor arguments. Calling the facade
or the helper before registering an adapter throws a
ViewException.
The global view() helper queues
the view, attaches the data and returns the output:
echo view('dashboard', ['username' => 'admin']);views/dashboard.php:
<h1>Welcome, <?= htmlspecialchars($username) ?></h1>Each data key becomes a local variable in the template. (Pure PHP does not auto-escape — escape untrusted values yourself.)
A list of names renders in order and concatenates the output — perfect for a header/content/footer layout:
echo view(['header', 'content', 'footer'], ['title' => 'Home']);The same data is available to every view in the batch.
$data is an associative array, or an object whose public properties become
the data:
$user = new stdClass();
$user->username = 'admin';
$user->roles = ['editor', 'author'];
echo view('profile', $user); // $username and $roles are available in the viewBecause every adapter shares the same interface, moving from plain PHP to Blade
is a one-line change at bootstrap — your view() calls stay the same:
use InitPHP\Views\Adapters\BladeAdapter;
View::via(new BladeAdapter(__DIR__ . '/views', __DIR__ . '/cache'));
// ...the rest of your code is unchanged.
echo view('dashboard', ['username' => 'admin']);See the adapter pages for each engine: Pure PHP, Blade, Twig.
The helper is optional — the facade exposes the same workflow fluently:
echo View::setView('header', 'footer')
->setData(['title' => 'Home'])
->render();You can also read merged data back:
View::setData(['title' => 'Home']);
View::getData('title'); // "Home"
View::getData('missing', '—'); // "—"
View::getData(); // ['title' => 'Home'] (everything)-
via()stored your adapter as the facade's active instance. -
setView()queued the view name(s);setData()merged your data. -
render()produced the output and then cleared the queue and data, so the adapter is clean for the next call.
-
State resets after every render. Two
view()calls never leak into each other:echo view('a', ['x' => 1]); // renders 'a' with x = 1 echo view('b', ['x' => 2]); // renders 'b' with x = 2 only
-
Pure PHP adds
.phpfor you.view('home')andview('home.php')both resolve toviews/home.php. Twig is the exception — include the full file extension in the name (e.g.view('home.html.twig')). -
Missing views throw. A Pure PHP view file that does not exist raises a
ViewException. -
Templates are sandboxed (Pure PHP). A view file sees only its data, never the adapter or
$this. See Pure PHP Adapter.
- The View Facade & Helper — the entry points in detail.
- Recipes — layouts, shared data, escaping, controller integration.
- API Reference — every public member.
initphp/views · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Adapters
Reference
Guides
Other