CMS blocks help to manipulate content of the Magento 2 stores. Static blocks can carry a simple text or html, CSS, Javascript to display content. It makes the reuse of the content in a modular and manageable way.
Previously, I had come up with the solution to Call CMS Static Block in Phtml File in Magento 2. Today, I will help you get static block content in block file in Magento 2.
Steps to Get Static Block Content in Block File in Magento 2:
1. Create Index.php file at Vendor\Extension\Block folder
namespace Vendor\Extension\Block; use Magento\Framework\View\Element\Template; use Magento\Cms\Block\Block as cmsBlock class Index extends Template { public $cms; public function __construct(Template\Context $context,cmsBlock $cmsblock) { $this->cmsblock = $cmsblock; parent::__construct($context); } public function getCmsBlock() { $cmsblock = $this->cmsblock->setBlockId('block_identifier')->toHtml(); return $cmsblock; } }
2. Write below code to get cmsBlock data in your phtml file
$block->getCmsBlock();
Simply following the above steps will help you get static block content in Magento 2 block file.
Also read: Magento 2 API – Get CMS Page Content
Happy Coding 🙂