NativePhpRenderer renders PHP template files using output buffering and explicit helper variables.
use CommonPHP\UI\Drivers\NativePhpRenderer;
$renderer = new NativePhpRenderer([
__DIR__ . '/templates',
]);You can add paths later:
$renderer->addPath(__DIR__ . '/more-templates');
$paths = $renderer->paths();The renderer searches:
- an explicit template path when
Template::file()or equivalent metadata is used; - the raw template name;
- the dotted-name path form;
- each configured template directory with those names;
- the same candidates with
.phpappended when missing.
For example, pages.profile can resolve to:
pages.profile
pages/profile
pages.profile.php
pages/profile.php
templates/pages.profile
templates/pages/profile
templates/pages.profile.php
templates/pages/profile.php
Template data is extracted into local variables:
<h1><?= $e($title) ?></h1>The renderer also provides helper variables:
$viewData, the fullViewDatainstance;$renderer, the activeNativePhpRenderer;$components, the active component registry;$component, a callable for rendering components;$escapeand$e, HTML escaping callables.
Helpers cannot be overwritten by user data because extraction uses EXTR_SKIP.
Use $e() or $escape() for HTML text and attribute values:
<span title="<?= $e($title) ?>"><?= $e($label) ?></span>The helper uses htmlspecialchars() with UTF-8 and substitute behavior.
Layouts are rendered after the view body. The view body is assigned to the layout's content key.
use CommonPHP\UI\Layout;
use CommonPHP\UI\View;
$view = new View(
'pages.profile',
['name' => 'Ada'],
new Layout('layouts.app'),
);<!-- templates/layouts/app.php -->
<main>
<?= $content ?>
</main><?= $component('badge', ['label' => 'Ready']) ?>If a component is passed as an object instead of a name, it does not need to be registered first.