Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 789 Bytes

File metadata and controls

54 lines (41 loc) · 789 Bytes

Components And Layouts

Directory structure:

templates/
  components/
    badge.php
  layouts/
    app.php
  pages/
    dashboard.php

Component:

<!-- templates/components/badge.php -->
<span class="badge"><?= $e($label) ?></span>

Page:

<!-- templates/pages/dashboard.php -->
<h1><?= $e($title) ?></h1>
<?= $component('badge', ['label' => 'Ready']) ?>

Layout:

<!-- templates/layouts/app.php -->
<main>
<?= $content ?>
</main>

Render:

use CommonPHP\UI\Component;
use CommonPHP\UI\ViewFactory;

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

$ui->registerComponent(new Component('badge', 'components.badge'));

$html = $ui->render(
    'pages.dashboard',
    ['title' => 'Dashboard'],
    'layouts.app',
);