-
-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ
- No dependencies, full control, small/medium apps: Pure PHP.
-
Rich templating with layouts, components and directives: Blade
(needs
illuminate/view). -
Designer-friendly, sandboxed templates: Twig (needs
twig/twig).
All three implement the same interface, so you can start with Pure PHP and switch later by changing one line at bootstrap. See the comparison table on the Home page.
No. The helper is a convenience wrapper over the View facade. Use the facade
directly if you prefer, or if another view() function already exists in your
project (the helper is guarded by function_exists, so it yields to an existing
one):
echo View::setView('home')->setData(['title' => 'Hi'])->render();You called the facade or the view() helper before
View::via().
Register an adapter once during bootstrap, before any rendering. The error is a
ViewException.
Yes — that is the point of the package. Your code calls view() or the facade;
only the View::via(...) line at bootstrap names the engine. Change that line
and everything else stays the same.
It depends on the adapter:
-
Pure PHP does not auto-escape — escape untrusted
data yourself with
htmlspecialchars()(orinitphp/escaper). -
Blade escapes
{{ }}output by default. - Twig auto-escapes by default.
The Pure PHP adapter only appends .php when it is missing, so view('home')
and view('home.php') both resolve to views/home.php. (This was a real bug in
1.0 and is fixed.)
No. With the Pure PHP adapter, each
view runs in an isolated scope that sees only its data variables — never $this
or the renderer's internals. A data key named views simply becomes $views in
the template.
Yes. An object's public properties become the template data:
$user = new stdClass();
$user->name = 'admin';
echo view('profile', $user); // $name is availableThe facade holds one active adapter; via() overwrites it. If you need two
engines in the same request, construct the adapters directly and call their
setView()/setData()/render() methods instead of going through the facade.
via() can only instantiate a class name when the adapter has a no-argument
constructor. The built-in adapters need directories, so pass a configured
instance instead:
View::via(new PurePHPAdapter(__DIR__ . '/views'));The class-name form is meant for your own zero-argument adapters. The error is a
ViewAdapterException.
Always a string. view(...) and View::render() return the output; you
decide whether to echo it, write it to a PSR-7 response, or use it as an email
body. See Recipes.
Yes. There is no framework requirement. The Blade adapter even bootstraps a standalone Illuminate container for you, so Blade works without a Laravel app — see Blade Adapter.
Twig resolves templates by their exact filename and does not assume an
extension, so you pass view('page.html.twig'). Only the
Pure PHP adapter adds .php
for you.
No — there is no PSR for view rendering. The package defines its own minimal
ViewAdapterInterface and the exceptions
implement a local ViewExceptionInterface marker.
Open an issue or start a discussion — documentation fixes are merged eagerly.
initphp/views · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Adapters
Reference
Guides
Other