:::: MENU ::::

Magento 2 Render WYSIWYG content

Magento 2 Render WYSIWYG content

Today we discuss how in Magento 2 Render WYSIWYG content. This topic will cover how to render wysiwyg editor content.There are two ways to render the WYSIWYG content. First Zend_Filter_Interface and second Using the \Magento\Cms\Model\Template\FilterProvider.

Using Zend Interface

namespace QaisarSatti\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $contentProcessor;
public function __construct(
    \Zend_Filter_Interface $templateProcessor,
) {
 
    $this->contentProcessor = $contentProcessor;
}
public function processContent($content)
{
    return $this->contentProcessor->filter($content);
}
}

Now Add block reference in di.xml

<type name="QaisarSatti\HelloWorld\Block\HelloWorld">
    <arguments>
        <argument name="contentProcessor" xsi:type="object">Magento\Widget\Model\Template\Filter</argument>
    </arguments>
</type>

Now call method from phtml

<?php echo $block->processContent($content);?>

Using CMS Model

namespace QaisarSatti\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $contentProcessor;
public function __construct(
    \Magento\Cms\Model\Template\FilterProvider $contentProcessor,
) {
 
    $this->contentProcessor = $contentProcessor;
}
public function processContent($content)
{
    return $this->contentProcessor->getPageFilter()->filter($content);
}
}

Now call method from phtml

<?php echo $block->processContent($content);?>
Author: Qaisar Satti
Category: Magento 2
Last Modified: April 4, 2018


So, what do you think ?