In this topic, I really want to introduce you the way via php code to get a block from template
phtml file in Magento 2. There are two examples I will give you here.
If you need to call a template block
Mageplaza_HelloWorld, please use the below code:
echo $this->getLayout() ->createBlock('Mageplaza\HelloWorld\Block\HelloWorld') ->setTemplate('Mageplaza_HelloWorld::helloworld.phtml') ->toHtml();
If you need to call template block in CMS static Block or CMS Page in Magento 2, please use the below code:
Hope this is the helpful article to get block from template
phtml file. In case that you have any queries about the article or any questions in general, use the comment section below!