38

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?

4 Answers 4

82

Try like this.

For ex your block class is

<?php
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.

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

Hope this helps you.

0
7

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 :

$block->myMethod();
4
  • @ Raphael is it $block->myMethod(); OR $this->myMethod(); ? Commented Jun 17, 2016 at 9:21
  • 4
    @AshishMadankar for Magento 1 it's $this->myMethod(), for Magento 2 it's $block->myMethod() Commented Jun 17, 2016 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
    Commented Jun 17, 2016 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. Commented Jun 17, 2016 at 9:30
1

Place following code in any phtml file:-

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

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).

App/Code/Your/Module/Block/Your_block_file.php

 <?php

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">
    <head>
        <title>{Page Title</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Your/Module/Block/Your_block_file" name="gridpage.form" template="Your_Module:: your_template.phtml"/>
        </referenceContainer>
    </body>
</page>

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

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.