3

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 ?

6

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();
  • I use this code but getting an error "Element with ID 'product.info.main_schedule_block5.state' is already defined." – Krupali Apr 8 '16 at 4:09
  • please check my updated question. – Krupali Apr 8 '16 at 6:36
  • Then the "state" alias is already used or the code is called twice. Try using a different alias, or if you use the code in a loop, append an incrementing number. – Fabian Schmengler Apr 8 '16 at 6:51
  • But for the sake of completeness, I added a solution for your updated question as well. – Fabian Schmengler Apr 8 '16 at 6:54
  • I try your updated answer.But it gives me blank page. I also delete var folder and then try. – Krupali Apr 8 '16 at 7:35
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();
?>
  • there any other way to accomplish my query ? Because I have 4 - 5 files which is calling state.phtml file. – Krupali Apr 8 '16 at 4:11
0

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 !

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.