Sometimes we need to use a static block in fronted to show some HTML content. For example, I have a static block with an image that I need to show on the product page. But by default when you call static block you can get only HTML text from the static block. like <img src="{{media url='test/Screenshot.png'}}" />
To convert this static block variables we need to use class \Magento\Cms\Model\Template\FilterProvider
Here is the sample code to show how you can convert block variable content to html
<?php protected $filter; public function __construct( ... \Magento\Cms\Model\Template\FilterProvider $filter ... ) { $this->filter = $filter; } public function filterStaticBlockContent($blockContent) { return $this->filter->getBlockFilter()->filter($content); }
Now you can use function filterStaticBlockContent
to get HTML content from the static block
$blockContent = "STATIC_BLOCK_CONTENT"; echo $this->filterStaticBlockContent($blockContent);
It will convert block variables to HTML content. like convert <img src="{{media url='test/Screenshot.png'}}" />
To<img src="https://www.example.com/pub/media/myimage/Screenshot.png" />
Hope this article will help you to understand filter CMS page or static block content .
Keep liking and sharing. Happy Coding 🙂