oberon / quill-rendering
Render 'insert' Delta's of a Quill Editor. Inspired by D. Blackborough's Quill Renderer package
Installs: 15 878
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/oberon/quill-rendering
Requires
- php: >=5.6.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^5
This package is auto-updated.
Last update: 2025-10-14 00:56:33 UTC
README
Installation
Use composer
composer require oberon/quill-rendering
Usage
$quillOps = "{\"ops\":[
    {\"insert\":\"Lorem ipsum dolor sit amet, \"},
    {\"insert\":\" consectetur\",\"attributes\":{\"bold\":true}},
    {\"insert\":\" adipiscing elit. Sed volutpat lectus non \"},
    {\"insert\":\"pellentesque volutpat\",\"attributes\":{\"italic\":true}},
    {\"insert\":\". Phasellus in lectus pulvinar lorem vestibulum pellentesque.\"}
]}";
try {
    $quill = new RenderQuill();
    $quill->setParsers(\Oberon\Quill\Render\Html\DefaultHtmlParsers::get());
    $quill->load($quillOps);
    echo $quill->render(true);
} catch (Exception $e) {
    echo $e->getMessage();
}
Output should be:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed volutpat lectus non pellentesque volutpat. Phasellus in lectus pulvinar lorem vestibulum pellentesque.
Most important components
- 
Renderer Has function renderwhich returns a string with formatted contents
- 
Parser When given a Quill opandrenderers[]returntrueifopwas handled and an appropriateRendererwas added to providedrenderers[],falseotherwise
Convenient classes/methods
DefaultHtmlParsers::get() returns a Parser[] for most basic HTML functionality including
- Bold
- Underline
- Strikethrough
- Italic
- Ordered/Unordered lists
- Links
- Images
- Headers
- Sub/SuperScript
HtmlParser::withQuill($quillOps) will render the provided Quill-text with the DefaultHtmlParsers.
Adding a custom Parser and Renderer
Example: You want to use <b> instead of <strong> for bold.
- Create a Renderer
class Bold implements Renderer {
	
	private $text;
	
	public function __construct($insert) {
		$this->text = $insert;
	}
	
	public function render() {
		return '<b>'.$this->text.'</b>';
	}
}
- Create a Parser
class BoldParser implements Parser {
	
	public function handleOp(array $op, array & $renderers) {
		
		//Check if your parser is going to handle the op
		if (array_key_exists('attributes', $op)
			&& is_array($op['attributes'])
			&& is_string($op['insert'])
			&& array_key_exists(Attribute::BOLD, $op['attributes'])) {
			
			//Instantiate your Bold Renderer
			$boldRenderer = new Bold($op['insert']);
			
			//It is inline, so add it to a Paragraph-block. The InlineUtil can do that
			InlineUtil::addToParagraph($boldRenderer, $renderers);
			
			return true;
		} else {
			// If your parser does not handle the op return false;
			return false;
		}
	}
}
- Register the Parser with the RenderQuill
//Start with the other HTML parsers
$parsers = DefaultHtmlParsers::get();
//Add to the beginning of the array,
//so it is checked before BasicHtmlParser which would also handle the bold attribute
$parsers = array_unshift($parsers, new BoldParser());
$quill = new RenderQuill();
//register
$quill->setParsers($parsers);
Note: This code is an example and needs modification if you would use it. For example: this does not handle cases where the attributes contain bold and some other attribute.
Notes
The load method immediately uses the provided Parsers, so Parsers should be set first.
Other Ops than insert exist: those will not be rendered. Use the Quill-Delta library at https://packagist.org/packages/oberon/quill-delta