Skip to content

Latest commit

 

History

History
67 lines (49 loc) · 1.24 KB

File metadata and controls

67 lines (49 loc) · 1.24 KB

Getting Started

CommonPHP UI renders a View through a renderer driver. The fastest path is ViewFactory with the native PHP renderer.

Create Templates

Create a PHP template:

<!-- templates/pages/home.php -->
<h1><?= $e($title) ?></h1>
<p><?= $e($message) ?></p>

Create an optional layout:

<!-- templates/layouts/main.php -->
<!doctype html>
<html>
<body>
<?= $content ?>
</body>
</html>

Render A View

use CommonPHP\UI\ViewFactory;

$ui = new ViewFactory(templatePaths: [__DIR__ . '/templates']);

$html = $ui->render(
    'pages.home',
    [
        'title' => 'Dashboard',
        'message' => 'Welcome back.',
    ],
    'layouts.main',
);

Template names can be dotted (pages.home) or path-like (pages/home.php). Dotted names are converted into directory paths by the native renderer.

Register A Component

use CommonPHP\UI\Component;

$ui->registerComponent(new Component('badge', 'components.badge', [
    'label' => 'New',
]));

Then call it inside a native PHP template:

<?= $component('badge', ['label' => 'Ready']) ?>

Next Steps