Magento2: How to call custom block in phtml file
-
05-02-2021 - |
Question
I have created a custom block page. I want to call custom block to another phtml file.
When I called custom block in xml file is working.
But when I called custom block in phtml file is not working.
<?php echo $block->getLayout()
->createBlock('Codism\Csr\Block\Index\TopMenu')
->setBlockId('Codism_Csr::menu.phtml')
->toHtml();
?>
Solution
Call phtml like this below way :
<?php echo $block->getLayout()
->createBlock('Codism\Csr\Block\Index\TopMenu')
->setTemplate('Codism_Csr::menu.phtml')
->toHtml();
?>
Or
<?php echo $this->getLayout()
->createBlock('Codism\Csr\Block\Index\TopMenu')
->setTemplate('Codism_Csr::menu.phtml')
->toHtml();
?>
OTHER TIPS
try this -
echo $block->getLayout()
->createBlock('Codism\Csr\Block\Index\TopMenu')
->setTemplate('Codism_Csr::menu.phtml')
->toHtml();
You are not using setTemplate
.
You need to use setTemplate instead of setBlockId
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange