An extendable PHP renderer for the Editor.js which works well with the Editor.js PHP backend.
Install the package using Composer:
composer require bpstr/editorjs-renderer-php:dev-master
use Bpstr\EditorJs\EditorJsRenderer;
$renderer = EditorJsRenderer::withBlocks($blocks);
The EditorJsRenderer class uses two required parameters:
mapping
: The block classes (or instances) keyed by the block type from the CodeX Editor.
Bpstr\EditorJs\EditorJsRenderer::$default_mapping = [
'header' => HeaderBlock::class,
'image' => ImageBlock::class,
'paragraph' => ParagraphBlock::class,
'quote' => QuoteBlock::class,
// ...
];
blocks
: Array having the same structure as one returned byEditorJS::getBlocks()
$blocks = [
['type' => 'heading', 'data' => ['text' => 'Dolor sit amet.']],
['type' => 'paragraph', 'data' => ['text' => 'Lorem ipsum']]
];
Almost every Editor.Js block plugin already have the matching classes bundled and tested in the package.
In case you would like to alter the Renderer, you can add or replace the mapped renderer blocks.
Calling this method will create instances of all the default classes plus the ones you provide.
$renderer = EditorJsRenderer::withBlocks($blocks, ['code' => ColoredCodeBlock::class]);
Using this method, no defaults will be added, the passed array will replace the inner mapping.
$renderer = EditorJsRenderer::withMapping(['code' => ColoredCodeBlock::class]);
You can add block instances to the Renderer instance on the fly too.
$renderer->map('header', new \Bpstr\EditorJs\Block\HeaderBlock());
To get the HTML markup, just call the render()
method.
echo $renderer->render();