PHP code example of nomadicjosh / vihzhuo

1. Go to this page and download the library: Download nomadicjosh/vihzhuo 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/ */

    

nomadicjosh / vihzhuo example snippets




use PDO;

return [
    'storage' => [
        'use_database' => true,
        'database' => [
            'driver' => 'mysql',
            'dsn' => 'mysql:host=127.0.0.1;dbname=website;charset=utf8mb4',
            'username' => 'website',
            'password' => $_ENV['DB_PASSWORD'],
            'prefix' => 'cms_',
            'options' => [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_EMULATE_PREPARES => false,
            ],
        ],
        'uploads_folder' => dirname(__DIR__) . '/storage/uploads',
    ],

    // Include the remaining sections from config.example.php.
];



declare(strict_types=1);

use Qubus\Http\Emitter\SapiEmitter;
use Qubus\Http\ServerRequestFactory;
use Vihzhuo\Vihzhuo;

onfiguration must return an array.');
}

$vihzhuo = new Vihzhuo($config);
$request = ServerRequestFactory::fromGlobals();
$response = $vihzhuo->handleRequest($request);

new SapiEmitter()->emit($response);

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Vihzhuo\Vihzhuo;

final class PageBuilderRoute
{
    public function __construct(private readonly Vihzhuo $vihzhuo)
    {
    }

    public function __invoke(ServerRequestInterface $request): ResponseInterface
    {
        return $this->vihzhuo->handleRequest($request);
    }
}

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Vihzhuo\Vihzhuo;

/** @return ResponseInterface|null */
function handleCmsRequest(
    Vihzhuo $vihzhuo,
    ServerRequestInterface $request,
    bool $userCanManagePages
): ?ResponseInterface {
    // Public pages, Vihzhuo assets, and uploaded images.
    $publicResponse = $vihzhuo->handlePublicRequest($request);
    if ($publicResponse !== null) {
        return $publicResponse;
    }

    // Apply the host application's authentication/authorization before this.
    if ($userCanManagePages) {
        $managerResponse = $vihzhuo->handleAuthenticatedRequest($request);
        if ($managerResponse !== null) {
            return $managerResponse;
        }
    }

    return null; // Let the next application route or middleware run.
}

use Vihzhuo\Contracts\PageContract;

/** @var PageContract $page */
$response = $vihzhuo->renderPageBuilder($page);

$html = $vihzhuo->getPageBuilder()?->renderPage($page, 'en') ?? '';

return [
    'auth' => [
        'use_login' => true,
        'class' => App\PageBuilder\CurrentUserAuth::class,
        'url' => '/cms/login',
    ],
    'router' => [
        'class' => App\PageBuilder\ApplicationRouter::class,
    ],
    // ...
];

$vihzhuo->setAuth($auth);
$vihzhuo->setRouter($router);
$vihzhuo->setTheme($theme);
$vihzhuo->setPageBuilder($pageBuilder);
$vihzhuo->setWebsiteManager($websiteManager);

use Vihzhuo\Modules\GrapesJS\Block\BlockAdapter;

'class_replacements' => [
    BlockAdapter::class => App\PageBuilder\CustomBlockAdapter::class,
],

'theme' => [
    'class' => Vihzhuo\Theme::class,
    'folder' => dirname(__DIR__) . '/public/themes',
    'folder_url' => '/themes',
    'active_theme' => 'acme',
],



return [
    'pagebuilder.default-category' => 'Acme blocks',
];

<img src="<?= phpb_theme_asset('images/logo.svg') 



return [
    'title' => 'Main layout',
];

<!doctype html>
<html lang="<?= phpb_e(phpb_current_language()) 



return [
    'category' => 'Marketing',
    'title' => 'Hero',
    'icon' => 'fa fa-star',
];



return [
    'category' => 'Marketing',
    'title' => 'Callout',
    'icon' => 'fa fa-bullhorn',
    'wrapper' => 'section',
    'cache' => true,
    'cache_lifetime' => 60,
    'settings' => [
        'heading' => [
            'type' => 'text',
            'label' => 'Heading',
            'value' => 'Ready to get started?',
            'placeholder' => 'Callout heading',
        ],
        'tone' => [
            'type' => 'select',
            'label' => 'Tone',
            'options' => [
                ['value' => 'callout-info', 'label' => 'Information'],
                ['value' => 'callout-success', 'label' => 'Success'],
                ['value' => 'callout-warning', 'label' => 'Warning'],
            ],
            'value' => 'callout-info',
        ],
        'show_link' => [
            'type' => 'yes_no',
            'label' => 'Show link',
            'value' => '1',
        ],
        'link_url' => [
            'type' => 'text',
            'label' => 'Link URL',
            'value' => '/contact',
        ],
    ],
];


$showLink = $block->setting('show_link') === '1';

return [
    'cache' => true,
    'cache_lifetime' => 15,
];

return [
    'title' => 'Customer portal',
    'whitelist' => ['customer.example.com', 'portal.test'],
];



return [
    'title' => 'Field examples',
    'settings' => [
        'headline' => [
            'type' => 'text',
            'label' => 'Headline',
            'value' => 'Hello world',
            'placeholder' => 'Enter a headline',
        ],
        'columns' => [
            'type' => 'number',
            'label' => 'Columns',
            'value' => '3',
        ],
        'variant' => [
            'type' => 'select',
            'label' => 'Variant',
            'options' => [
                ['value' => 'light', 'label' => 'Light'],
                ['value' => 'dark', 'label' => 'Dark'],
            ],
            'value' => 'light',
        ],
        'featured' => [
            'type' => 'checkbox',
            'label' => 'Featured',
            'value' => '',
        ],
        'show_border' => [
            'type' => 'yes_no',
            'label' => 'Show border',
            'value' => '1',
        ],
        'accent_color' => [
            'type' => 'color',
            'label' => 'Accent color',
            'value' => '#2563eb',
        ],
    ],
];

'options' => [
    ['id' => 'sm', 'name' => 'Small'],
    ['id' => 'lg', 'name' => 'Large'],
],

use Vihzhuo\ThemeBlock;

ThemeBlock::set('callout', 'settings', [
    'heading' => [
        'type' => 'text',
        'label' => 'Heading',
        'value' => 'Application default',
    ],
]);

<section class="alert">
    [block slug="alert-text" id="message"]
</section>

 for ($index = 0; $index < 3; $index++): 

<section class="columns">
    <div class="column">[blocks-container]</div>
    <div class="column">[blocks-container]</div>
</section>

return [
    'title' => 'Latest posts',
    'namespace' => 'App\\PageBuilder\\Blocks\\LatestPosts',
];



declare(strict_types=1);

namespace App\PageBuilder\Blocks\LatestPosts;

use Vihzhuo\Modules\GrapesJS\Block\BaseModel;

final class Model extends BaseModel
{
    /** @var list<array{title: string, url: string}> */
    private array $posts = [];

    protected function init(): void
    {
        $this->posts = [
            ['title' => 'First post', 'url' => '/blog/first-post'],
        ];
    }

    // Optional editor-only initialization for a fast or deterministic preview.
    protected function initEdit(): void
    {
        $this->posts = [
            ['title' => 'Example post', 'url' => '#'],
        ];
    }

    /** @return list<array{title: string, url: string}> */
    public function posts(): array
    {
        return $this->posts;
    }
}

<ul class="latest-posts">
     foreach ($block->posts() as $post): 



declare(strict_types=1);

namespace App\PageBuilder\Blocks\LatestPosts;

use Vihzhuo\Modules\GrapesJS\Block\BaseController;

final class Controller extends BaseController
{
    public function handleRequest(): void
    {
        // Perform block-specific request coordination here.
        // The initialized model, page, and editor flag are protected properties.
    }
}

<link rel="stylesheet" href="<?= phpb_theme_asset('css/theme.css') 

use Vihzhuo\Extensions;

Extensions::registerAsset(
    '/build/page-blocks.css',
    'style',
    'header',
    ['media' => 'screen']
);

Extensions::registerAsset(
    '/build/page-blocks.js',
    'script',
    'footer',
    ['defer' => 'defer']
);

<head>
     phpb_registered_assets('header'); 

$pageBuilder = $vihzhuo->getPageBuilder();

$pageBuilder?->customStyle(<<<'HTML'
<style>
  .gjs-pn-panels { --brand-accent: #7c3aed; }
</style>
HTML);

$pageBuilder?->customScripts('head', <<<'HTML'
<script>
  window.addEventListener('vihzhuo:grapesjs:ready', event => {
    console.log('Editor ready', event.detail);
  });
</script>
HTML);

use Vihzhuo\Extensions;

Extensions::registerBlock(
    'pricing-table',
    __DIR__ . '/PageBuilder/Blocks/PricingTable'
);

Extensions::registerLayout(
    'campaign',
    __DIR__ . '/PageBuilder/Layouts/Campaign'
);

Extensions::addBlocks([
    'pricing-table' => __DIR__ . '/Blocks/PricingTable',
    'testimonial' => __DIR__ . '/Blocks/Testimonial',
]);

Extensions::addLayouts([
    'campaign' => __DIR__ . '/Layouts/Campaign',
]);

$slug = phpb_route_parameter('slug');

$parameters = phpb_route_parameters();

phpb_current_language();
phpb_active_languages();
phpb_current_relative_url();
phpb_full_url('/contact');
phpb_url('pagebuilder', ['page' => $page->getId()]);
phpb_pages();

'cache' => [
    'enabled' => true,
    'folder' => dirname(__DIR__) . '/storage/page-cache',
    'class' => Vihzhuo\Cache::class,
],

return [
    'cache' => true,
    'cache_lifetime' => 15, // minutes
];

return [
    'cache' => false,
];
bash
cp config/config.example.php config/config.php
text
builder-script.js
builder-script.html
builder-script.php
text
/about
/blog/{slug}
/documentation/*