Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
106 views

Magento2 - Magento 2 How To Call Any Block Function in PHTML

The document discusses how to call a custom block function from a phtml template file in Magento 2. It provides the following steps: 1. Create a block class that extends the template block and defines the custom method. 2. Assign the block to the template file in the XML layout. 3. Call the custom method on the block object in the phtml file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Magento2 - Magento 2 How To Call Any Block Function in PHTML

The document discusses how to call a custom block function from a phtml template file in Magento 2. It provides the following steps: 1. Create a block class that extends the template block and defines the custom method. 2. Assign the block to the template file in the XML layout. 3. Call the custom method on the block object in the phtml file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1/10/2020 magento2 - magento 2 how to call any block function in phtml - Magento Stack Exchange

What's the deal with Deno? We talk with a major contributor to find out. Listen now.

Magento Stack Exchange is a question


and answer site for users of the Magento
e-Commerce platform. It only takes a
minute to sign up.

Sign up to join this community

Anybody can ask a question

Anybody can answer

The best answers are voted


up and rise to the top

magento 2 how to call any block function in phtml


Asked 4 years, 3 months ago Active 7 months ago Viewed 58k times

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?
37
magento2 blocks template

edited Jun 17 '16 at 9:18 asked Jun 17 '16 at 9:13


12
Raphael at Digital Arshad M
Pianism 824 1 9 13
65.7k 24 163 332

4 Answers Active Oldest Votes

Try like this.

71 For ex your block class is

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
<?php
our Terms namespace
of Service. Company\Helloworld\Block;

https://magento.stackexchange.com/questions/121364/magento-2-how-to-call-any-block-function-in-phtml 1/4
1/10/2020 magento2 - magento 2 how to call any block function in phtml - Magento Stack Exchange
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.

answered Jun 17 '16 at 10:57


Ashish Madankar M2
Professiona
3,255 3 13 36

working like a charm ,, thank u – Hafiz Arslan Jul 3 '19 at 7:23

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

6 <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();

answered Jun 17 '16 at 9:17


Raphael at Digital
Pianism
65.7k 24 163 332

@ 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
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of @RaphaelatDigitalPianism
Service. 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
https://magento.stackexchange.com/questions/121364/magento-2-how-to-call-any-block-function-in-phtml 2/4
1/10/2020 magento2 - magento 2 how to call any block function in phtml - Magento Stack Exchange

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

<head>
<title
By using our site, you >{Page Title
acknowledge that</title
you > read and understand our Cookie Policy, Privacy Policy, and
have
</head>
our Terms of Service
<body>
.

https://magento.stackexchange.com/questions/121364/magento-2-how-to-call-any-block-function-in-phtml 3/4
1/10/2020 magento2 - magento 2 how to call any block function in phtml - Magento Stack Exchange
<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

answered Oct 31 '18 at 6:06


Manish Kumar
373 1 15

Place following code in any phtml file:-

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

answered Feb 5 at 13:05


Girase Priyanka
1 2

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://magento.stackexchange.com/questions/121364/magento-2-how-to-call-any-block-function-in-phtml 4/4

You might also like