Magento 2.x – How to call any block function inside a phtml
If you are inside a phtml file, and you need to call an specific function inside another block. You can do the following:
- The block method that you want to call is the following:
1234567891011<?phpnamespace Vendor\MyModule\Block;use Magento\Framework\View\Element\Template;class MyClass extends Block_Class{public function myCustomMethod(){return '<b>Block Method i want to display</b>';}} - This is what you should do, in your phtml:
1234<?php$blockInstance= $block->getLayout()->createBlock('Vendor\MyModule\Block\MyClass');echo $blockInstance->myCustomMethod();?>
Hope it helps.