Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

How to parse content like CMS page in custom block in Magento 2

In this blog I’ll explain how can you parse content like CMS page. That means parsing the content of WYSIYG editor, parsing contents like shown below,

{{media url="wysiwyg/test.jpg"}}

{{config path="trans_email/ident_custom2/name"}}

{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/default.phtml" block_id="4"}}

{{block class="Magento\Framework\View\Element\Template" name="test" template="Magento_Catalog::product/widget/test.phtml"}}

To parse the content like these you need to get the \Magento\Cms\Model\Template\FilterProvider instance and call the getBlockFilter() or getPageFilter() as shown below,

public function __construct(
    .....
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    .....
) {
    .....
    $this->filterProvider = $filterProvider;
    .....
}
   
public function getCMSContent()
{
    .....
    $parsedContent = $this->filterProvider->getPageFilter()->filter($content);    //$content is the content that you want to parse
    return $parsedContent;
}

Thanks for reading the blog.

Feel free to comment if you face any issue.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home