Skip to content

Render HTML elements based on parsed EditorJs block data.

Notifications You must be signed in to change notification settings

bpstr/editorjs-renderer-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

editorjs-renderer-php

An extendable PHP renderer for the Editor.js which works well with the Editor.js PHP backend.

Getting Started

Installation

Install the package using Composer:

composer require bpstr/editorjs-renderer-php:dev-master

Basic usage

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 by EditorJS::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.

Rewrite the mapping

In case you would like to alter the Renderer, you can add or replace the mapped renderer blocks.

Providing static mapping

Calling this method will create instances of all the default classes plus the ones you provide.

$renderer = EditorJsRenderer::withBlocks($blocks, ['code' => ColoredCodeBlock::class]);

Replace mapping

Using this method, no defaults will be added, the passed array will replace the inner mapping.

$renderer = EditorJsRenderer::withMapping(['code' => ColoredCodeBlock::class]);

Mapping on the fly

You can add block instances to the Renderer instance on the fly too.

$renderer->map('header', new \Bpstr\EditorJs\Block\HeaderBlock());

Rendering blocks

To get the HTML markup, just call the render() method.

echo $renderer->render();

About

Render HTML elements based on parsed EditorJs block data.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages