Magento2: How to call custom block in phtml file
-
05-02-2021 - |
Domanda
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();
?>
Soluzione
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();
?>
Altri suggerimenti
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange