PHP code example of phpsoftbox / view

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/ */

    

phpsoftbox / view example snippets


use PhpSoftBox\View\PhpViewRenderer;

$renderer = new PhpViewRenderer(
    basePath: __DIR__ . '/resources/views',
    sharedData: [
        'appName' => 'Demo',
    ],
);

echo $renderer->render('error.phtml', [
    'status' => 500,
    'message' => 'Internal error',
]);

use PhpSoftBox\View\ViewDataInterface;

final readonly class UserCardView implements ViewDataInterface
{
    public function __construct(
        public string $name,
        public string $email,
    ) {
    }
}

echo $renderer->render('user/show.phtml', [
    'name' => 'Anton',
]);


/** @var string $name */
/** @var \PhpSoftBox\View\ViewContext $viewContext */

$viewContext->setLayout('layouts/app.phtml', [
    'title' => 'User page',
]);


/** @var string $content */
/** @var string|null $title */

$view = new UserCardView(name: 'Anton', email: '[email protected]');

echo $renderer->render('user/card.phtml', $view);


/** @var UserCardView $this */

 $viewContext->setLayout('layouts/admin.phtml', ['title' => 'Users']); 

 $viewContext->setLayout('layouts/app.phtml', ['title' => $title]); 

$badge = $renderer->partialRender('partials/badge.phtml', [
    'label' => 'New',
]);

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);
$context->addMeta('robots', 'noindex');

$html = $renderer->partialRenderWithContext('partials/header.phtml', [
    'title' => 'Settings',
], $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)