0

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();
?>
  • Is there any error or system.log? – Rohan Hapani Nov 5 '18 at 8:47
  • No errors in system log file.\ – Masud Shaikh Nov 5 '18 at 8:51
  • Please check my answer. – Rohan Hapani Nov 5 '18 at 8:53
3

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();
?>
  • What is diff in my code? – Masud Shaikh Nov 5 '18 at 8:54
  • You need to use setTemplate() instead of setBlockId() – Rohan Hapani Nov 5 '18 at 8:55
  • Happy to help !! Happy coding :) – Rohan Hapani Nov 5 '18 at 8:56
1

try this -

echo $block->getLayout()
          ->createBlock('Codism\Csr\Block\Index\TopMenu')
          ->setTemplate('Codism_Csr::menu.phtml')
          ->toHtml();

You are not using setTemplate.

1

You need to use setTemplate instead of setBlockId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.