1

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();
?>
3
  • Is there any error or system.log? Commented Nov 5, 2018 at 8:47
  • No errors in system log file.\ Commented Nov 5, 2018 at 8:51
  • Please check my answer. Commented Nov 5, 2018 at 8:53

3 Answers 3

7

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();
?>
3
  • What is diff in my code? Commented Nov 5, 2018 at 8:54
  • You need to use setTemplate() instead of setBlockId() Commented Nov 5, 2018 at 8:55
  • Happy to help !! Happy coding :) Commented Nov 5, 2018 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.

0
1

You need to use setTemplate instead of setBlockId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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