In today’s post, we are going to teach you how to convert CMS page content to HTML in Magento 2.
Sometimes, Magento developers need some or all of their CMS page content to HTML for adding static blocks or template file, or HTML code.
In such situations, the CMS pages are converted to HTML to perform these tasks.
Now, in case you didn’t know, the CMS page content generally uses the WYSIWYG editor in order to add new content to the CMS page.
The WYSIWYG editor is basically an addon in Magento 2, which is used for content editing in the backend, allowing users who don’t work with HTML to edit content.
Below, we will look at how to convert CMS page content to HTML in a step-by-step tutorial.
Steps to Convert CMS Page Content to HTML in Magento 2
Step 1
To convert your CMS page content to HTML, you will first need to use \Magento\Cms\Model\Template\FilterProvider class in order to filter the static content in blocks.
Step 2
Next, you will need to inject the following code in construct.
/** * @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); }
Step 3
Now, it’s time to call the template file to use HTML content:
<?php echo $block->getContentFromStaticBlock($content); ?>
As you’ll execute this command, your CMS page content will be converted to HTML.
Also read: How to Create Custom CMS Page Layout XML File in Magento 2?
Conclusion
There you have it! This is the easiest way to convert CMS page content to HTML in Magento 2.
We hope that you found this tutorial useful. And if we have missed anything in this tutorial, please let us know. Also, if you need our professional help, feel free to contact us anytime.