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);?>

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply