1

I have a block within a phtml file: the block is not associated with a controller.

echo $this->getLayout()
      ->createBlock("Top\CmsAndTemplatePages\Block\Cmspages")
      ->setTemplate("Top_CmsAndTemplatePages::categoryPages/rootblock.phtml")
      ->toHtml(); 

my question now is how do i call the methods of the block.

i.e within the class

Top-> CmsAndTemplatePages->Block->Cmspages is a method called

$this->_toHtml()

how do i access this method from the phtml page

1
0

If you want to call the method of Top\CmsAndTemplatePages\Block\Cmspages then try to below code:

  //Initiated   block class 
    $Cmspages = $block->getLayout()
          ->createBlock("Top\CmsAndTemplatePages\Block\Cmspages")

 //Call method of this class 
 $Cmspages->myTESTMethod();

Here myTESTMethod is a method of this class.

And if you want to print the html output of this block then use

<?= $Cmspages->toHtml() ?>
2
  • This works but you can only call public methods. I have a feeling _toHtml is a protected one.
    – Marius
    Jan 15 '19 at 14:17
  • OPPS, you can only class pUblic function not Protected and other
    – Amit Bera
    Jan 15 '19 at 14:21

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.