How can I call any block function in any phtml? For example if I want to call my custom block function in product list.phtml?


Try like this.

For ex your block class is

namespace Company\Helloworld\Block;
use Magento\Framework\View\Element\Template;

class Main extends Template
    public function getMyCustomMethod()
        return '<b>I Am From MyCustomMethod</b>';

then in any phtml file you can use following code to get method of this block.

$blockObj= $block->getLayout()->createBlock('Company\Helloworld\Block\Main');
echo $blockObj->getMyCustomMethod();

Hope this helps you.


If the template is linked to the block, for example:

<block class="Vendor\Module\Block\Name" name="name" template="Vendor_Module::name.phtml"/>

And you have a public method myMethod() defined in Vendor\Module\Block\Name you can call the following in name.phtml :

  • @ Raphael is it $block->myMethod(); OR $this->myMethod(); ? – Ashish Madankar M2 Professiona Jun 17 '16 at 9:21
  • 4
    @AshishMadankar for Magento 1 it's $this->myMethod(), for Magento 2 it's $block->myMethod() – Raphael at Digital Pianism Jun 17 '16 at 9:21
  • @RaphaelatDigitalPianism I want to call my custom block function in any phtml, basically my question is how can I create $myBlock object for any of my block. – Arshad M Jun 17 '16 at 9:24
  • You need to assign your block to the template.phtml in layout file. Then only you can call your own function in phtml. – Aman Srivastava Jun 17 '16 at 9:30

Place your block File in the root directory of your module /Block/Your_block_file.php(remember to user first capital Letter for folder and file).



namespace Your\Module\Block;

class Your_block_file extends \Magento\Framework\View\Element\Template
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry             $registry
     * @param \Magento\Framework\Data\FormFactory     $formFactory
     * @param array                                   $data
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
        parent::__construct($context, $data);

     * Get form action URL for POST booking request
     * @return string
    public function getFormAction()
        die('Hello World');

Then link your block file with template in view/frontend/layout/your_file.xml file you defined the block file

App/Code/Your/Module/view/frontend/layout/your_file.xml(if you are using routes.xml make sure the name of your file must be like for ex. frontname_controllerFolder_FileUnderControlerFolder.xml)

    <?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <title>{Page Title</title>
        <referenceContainer name="content">
            <block class="Your/Module/Block/Your_block_file" name="gridpage.form" template="Your_Module:: your_template.phtml"/>

Then Define your template file in App/Code/Your/Module/view/frontend/templates/your_template.phtml

<?= $block->getFormAction(); ?>

Thats how you can call Block functions in in template file


Place following code in any phtml file:-

$blockName = $block->getLayout()->createBlock('Vendor\Module\Block\your_block_file_name');
yourBlockFileMethodName(); ?>

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.