PHP code example of webx-ui / module-blocks

1. Go to this page and download the library: Download webx-ui/module-blocks 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/ */

    

webx-ui / module-blocks example snippets


Schema::table('pages', function (Blueprint $table) {
    $table->blocks();   // `blocks` json — the tree the site prints
    $table->draft();    // `draft` json and `published_at`, from webx-ui/module-admin
});

class Page extends Model
{
    use HasBlocks, HasDraft, HasVersions;
}

$page->blocks;          // [{ key, type, values }, …]
$page->renderBlocks();  // HtmlString
$page->blockTypes();    // ['hero', 'section', 'text'] — nested ones 

{!! Blocks::render($blocks, $page) !!}

$block = Block::create(['slug' => 'hero', 'title' => 'Hero', 'group' => 'content']);

$block->saveVersion([
    'schema'   => [['id' => 'title', 'type' => 'wx-input', 'label' => 'Title']],
    'template' => '<section data-wx-block="hero" class="b-hero"><h1>{{ $title }}</h1></section>',
    'styles'   => '.b-hero { padding: var(--wx-space-32) }',
    'sample'   => ['title' => 'Welcome'],
]);

$block->publish();

BlockTypes::all();          // enabled types with a published version, in sort order — the picker
BlockTypes::find('hero');   // the published version, disabled or not — what the site renders
BlockTypes::draft('hero');  // the draft, or the published one when there is none — the preview

Preview::url($page, adminId: 7);   // https://example.test/_preview/page/12?token=…