4

I have create a block which contains a phtml file.This .phtml file called other phtml file.

Please check code :

block name : ..\block\country.php

This block has a template file, name : country.phtml
Code in this file

<?php
    echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Namespace_Module::state.phtml")->toHtml();
?>

As you seen country.phtml file call state.phtml file.

Now issue is, if I want to use any function of parent block country.php in state.phtml file then I am getting an error.

So please help me how can I called functions of parent block country.php

Thank you.

If I set state.phtml in country.phtml like

<?php
echo $this->getLayout()->createBlock("Namespace\Module\Block\Country")->setTemplate("Namespace_Module::state.phtml")->toHtml();
?>

Then Is it possible to call functions of Country.php in State.phtml file ?

Asish Hira
  • 1,981
  • 1
  • 17
  • 39
Krupali
  • 379
  • 2
  • 9
  • 18

3 Answers3

7

With this code you create this block and render it immediately, without adding it to the layout hierarchy. That means it doesn't have access to the parent block because there is no parent.

To create a block dynamically as child of the current block, use:

$child = $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Namespace_Module::state.phtml");
$this->addChild('state', $child);

And to render it:

echo $this->getChildHtml('state');

(I used "state" as alias here, but it can be anything else)

Now you have access to the parent from your state.phtml template, using $this->getParentBlock()


If I set state.phtml in country.phtml like

<?php
echo $this->getLayout()->createBlock("Namespace\Module\Block\Country")->setTemplate("Namespace_Module::state.phtml")->toHtml();
?>

Then Is it possible to call functions of Country.php in State.phtml file ?

If you insist, you can do it like this:

 echo $this->getLayout()
     ->createBlock("Namespace\Module\Block\Country")
     ->setTemplate("Namespace_Module::state.phtml")
     ->setData('country_block', $this)
     ->toHtml();

And then, in state.phtml:

$this->getData('country_block')->METHOD_OF_COUNTRY_BLOCK();
Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421
1

You can easily access the parent block methods like:

$this->getParentBlock()->MethodName();

The $this->getParentBlock() will return the instance of Parent Block in child template file.

Cheers !

Shubham Mathur
  • 550
  • 4
  • 14
0

My suggestion:

  • Create a custom block for your state template
  • Make that block extend your country block
  • This way you can use $block->yourFunction() easily

Don't forget to update your code:

<?php
    echo $this->getLayout()->createBlock("Namespace\Module\State")->setTemplate("Namespace_Module::state.phtml")->toHtml();
?>
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
  • there any other way to accomplish my query ? Because I have 4 - 5 files which is calling state.phtml file. – Krupali Apr 08 '16 at 04:11