1. Go to this page and download the library: Download phpsoftbox/view library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use PhpSoftBox\View\ViewDataInterface;
final readonly class UserCardView implements ViewDataInterface
{
public function __construct(
public string $name,
public string $email,
) {
}
}
final readonly class BadgeView implements ViewDataInterface
{
public function __construct(public string $label) {}
}
$badge = $renderer->partialRender('partials/badge.phtml', new BadgeView('Hot'));
use PhpSoftBox\View\ViewContext;
$context = new ViewContext($renderer);
$context->addMeta('description', 'Admin page');
echo $renderer->renderWithContext('admin/index.phtml', [
'title' => 'Admin',
], $context);
$context = new ViewContext($renderer);
$context->addStyle('/assets/admin.css');
echo $renderer->renderWithContext('admin/profile.phtml', new UserCardView(
name: 'Anton',
email: '[email protected]',
), $context);
$context = new ViewContext($renderer);
$html = $renderer->partialRenderWithContext('partials/user-row.phtml', new UserCardView(
name: 'Anton',
email: '[email protected]',
), $context);
use PhpSoftBox\View\ViewContext;
use PhpSoftBox\View\ViewContextAwareInterface;
use PhpSoftBox\View\ViewDataInterface;
final readonly class LayoutView implements ViewDataInterface, ViewContextAwareInterface
{
public function __construct(
public string $content,
private ?ViewContext $viewContext = null,
) {
}
public function withViewContext(ViewContext $context): object
{
return new self($this->content, $context);
}
public function renderPartial(string $template, array|ViewDataInterface $data = []): string
{
return $this->viewContext?->partialRender($template, $data) ?? '';
}
}
<h1><?= html($title)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.