-
-
Notifications
You must be signed in to change notification settings - Fork 0
The View Facade and Helper
Two entry points sit in front of the active adapter: the static View facade
and the global view() helper. The helper is a thin wrapper over the facade, so
they share one behaviour.
InitPHP\Views\Facade\View is a static entry point to a single registered
adapter. It is intentionally static-only and cannot be instantiated.
public static function via(string|ViewAdapterInterface $adapter): voidvia() accepts either:
- a ready-to-use adapter instance (the usual choice — the built-in adapters take constructor arguments), or
- the class name of an adapter that can be constructed with no arguments.
use InitPHP\Views\Facade\View;
use InitPHP\Views\Adapters\PurePHPAdapter;
// Instance — required for the bundled adapters
View::via(new PurePHPAdapter(__DIR__ . '/views'));
// Class name — only for adapters with a no-argument constructor
View::via(MyZeroArgAdapter::class);Calling via() again replaces the active adapter. It throws a
ViewAdapterException when the argument is a class name that:
- does not exist,
- does not implement
ViewAdapterInterface, or - requires constructor arguments that a bare
new $class()cannot supply (pass a configured instance instead).
View::via(PurePHPAdapter::class);
// ViewAdapterException: PurePHPAdapter needs a $viewDir argument — pass an instance.Once an adapter is registered, every static call is forwarded to it:
View::setView('header', 'footer'); // queue views, in order
View::setData(['title' => 'Home']); // merge data
$all = View::getData(); // read all data
$one = View::getData('title', '—'); // read one value, with a default
echo View::render(); // render the queue and resetsetView() and setData() return the adapter, so they chain:
echo View::setView('dashboard')->setData(['user' => 'admin'])->render();Calling any of these before via() throws a ViewException.
For the Blade adapter, View also forwards engine-specific
methods such as directive() and any other Blade factory method.
The view() function is registered through Composer autoloading and is always
available:
function view(string|array $views, array|object $data = []): stringIt wraps the facade:
echo view('dashboard', ['username' => 'admin']);
// is exactly:
echo View::setView('dashboard')->setData(['username' => 'admin'])->render();A string renders one view; an array of strings renders several, in order:
echo view(['header', 'content', 'footer'], ['title' => 'Home']);The helper is defined behind a function_exists('view') guard, so it will not
collide with a view() function your framework may already provide. If another
view() is loaded first, the helper simply isn't defined — use the facade
directly in that case.
render() clears the queued views and merged data once it finishes — even if
a view throws — so the same adapter instance can be reused for independent
renders without state leaking between them:
echo view('a', ['x' => 1]); // renders 'a' with x = 1
echo view('b', ['x' => 2]); // renders 'b' with x = 2 only — nothing from the first callsetData() merges: repeated keys overwrite earlier values within a single
render.
View::setData(['a' => 1, 'b' => 2]);
View::setData(['b' => 3]); // 'b' is now 3
View::getData(); // ['a' => 1, 'b' => 3]public function getData(?string $key = null, mixed $default = null): mixed-
getData()orgetData(null)returns the whole data array. -
getData('key')returns the stored value, ornullif the key is absent. -
getData('key', $default)returns$defaultwhen the key is absent.
A stored null is distinct from a missing key — getData('present', 'x')
returns null if present was set to null, but 'x' if it was never set.
- API Reference — exact signatures.
-
Custom Adapters — what
via()expects of an adapter. - Exceptions — every error the facade can raise.
initphp/views · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Adapters
Reference
Guides
Other