Request Quote

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 CMG 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.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close