PHP code example of texditor / blockify-php

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

    

texditor / blockify-php example snippets


use Texditor\Blockify\Blockify;
use Texditor\Blockify\Config;
use Texditor\Blockify\Models\ParagraphBlock;
use Texditor\Blockify\Models\CodeBlock;
use Texditor\Blockify\Models\FileBlock;
use Texditor\Blockify\Models\ImageBlock;
use Texditor\Blockify\Models\HeaderBlock;
use Texditor\Blockify\Models\OrderedListBlock;
use Texditor\Blockify\Models\UnorderedListBlock;
use Texditor\Blockify\Models\TableBlock;
use Texditor\Blockify\Models\DividerBlock;

// Initialize configuration
$config = (new Config())
    ->addModels(
        (new ParagraphBlock())
          ->setAllowedTags(['a', 'b']),
        new CodeBlock(),
        (new FileBlock())
            ->setSourceProtocols(['https', 'http'])
            ->transformItem(function ($item) {
                $item['hello'] = 'world';
                return $item;
            }),
            ->setSourceHosts([
                'priveted.com', 
                'github.com'
            ])
            // or only locally
            ->setSourceRegex(["/^\/uploads\/.*\.(png|jpg|jpeg|gif)$/"]),
   
        (new DividerBlock()),
            ->setRenderAttribute('class', 'my-divider'),

        (new TableBlock())
            ->setMaxRows(50)
            ->setMaxCols(6)
            ->setMaxBreaks(2),

        (new ImageBlock())
            ->setIsLinkStrategy(true) // the old strategy, use IDs
            // ->setIsMeta(true)
            // ->setIsMetaCaption(true)
            // ->setIsMetaDesc(false)
            ->setSourceHosts([
                'priveted.com', 
                'myimageserver.com'
            ])
            ->setSourceProtocols(['https'])
            ->setMetaPosition('top')
            ->setImageTypes(['image/png', 'image/jpeg'])
            ->setVideoTypes(['video/mp4'])
            ->setVideoAttributes([
                'controls' => 'true'
            ]),

        new VideoBlock(),
        new UnorderedListBlock(),
        new OrderedListBlock(),
        // Default h1
        new HeaderBlock(),
        (new HeaderBlock())
            ->setOutputName('h2')
            ->setInputName('h2'),
            //... and other block model methods (Texditor\Blockify\BlockModel)
    );
// Dev mode
$config->setDev(true);

// Process data
$blockify = (new Blockify($config))
    ->setData($jsonData);

// Or if filters are needed
$blockify
    ->filter(function (array $block, int $index, BlockModelInterface $model) {
        if ($model->isPreformatted())
            return false;

        if ($block['type'] == 'p') 
            return false;

        return true;
    })
    ->dataFilter(function (
        array|string $item,
        int $index,
        array $block,
        int $blockIndex,
        BlockModelInterface $model
    ) {
        if (
            is_array($item) &&
            $block['type'] == 'gallery' &&
            empty($item['id'])
        ) {
            return false;
        }

        return true;
    });

// Get secure and processed data
$blocks = $blockify->getData();


//...
$blockify = new Blockify($config);

if (!$blockify->isValid()) {
    $errors = $blockify->getErrors();
    // Handle errors
}

use Texditor\Blockify\HtmlBuilder;

//...
// Initializes the HTML builder with security and rendering settings.
$htmlBuilder = new HtmlBuilder($config);

// Renders all provided blocks into a safe HTML string.
$html = $htmlBuilder->render($blocks);

// Renders all blocks into plain text, stripping all HTML tags.
$text = $htmlBuilder->renderAsText($blocks);

// Renders only the specified block types ('p', 'h1') into HTML.
$html = $htmlBuilder->renderOnly($blocks, ["p", "h1"]);

// Renders only the specified block types into plain text.
$text = $htmlBuilder->renderOnly($blocks, ["p", "h1"], true);
bash
composer