In this technical blog, We will learn about how to convert cms page content to HTML in Magento 2. CMS page content uses WYSIWYG editor to add content to the cms page. The WYSIWYG editor is an addon that is used for the backend for content editing and also allows people who don’t work with HTML to edit content.
Sometimes, we need to convert that cms page content. Because, we added static blocks in cms page content, template file or HTML code, etc.
To convert CMS page content to HTML, We need to use, \Magento\Cms\Model\Template\FilterProvider class to filter static content in Blocks.
You may also like my last blogs :
You need to inject in a construct like this below code :
/**
* Created By : Rohan Hapani
*/
/**
* @var \Magento\Cms\Model\Template\FilterProvider
*/
protected $_filterProvider;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->_filterProvider = $filterProvider;
$this->_storeManager = $storeManager;
}
/**
* Static block $content
*/
public function getContentFromStaticBlock($content)
{
$storeId = $this->_storeManager->getStore()->getId();
return $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($content);
}
Now, call in your template file to use HTML content :
<?php echo $block->getContentFromStaticBlock($content); ?>
Finally, You can convert CMS page content to HTML successfully.
I hope this blog is easy to understand how to convert CMS page content to HTML in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂