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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.