letsanov
letsanov

Reputation: 201

Calling child block methods in main block phtml

Basically what i want to is to include child blocks in my main block, and use their methods without making them input any html.

If i have this structure:

<block type="core/template" template"/custom.phtml">
  <block type="catalog/product_view_options" />
</block>

In custom.phtml i want to be able to call method from options block.

$this->some_method_from_options_block();

That way i wont need to use createBlock inside the custom.phtml every time i need to access some method

Upvotes: 0

Views: 1706

Answers (2)

Emi
Emi

Reputation: 1018

When you write in the xml:

<block type="core/template" template="custom.phtml">

Your block's class is Mage_Core_Block_Template and you can access any methods inside that class and the class it extends. So you can have 2 options here, your parent class extends your child class ( I believe this is what you want, but is a bit wrong ). Inside Magento you'll see that for this you have something like:

<block type="core/template" template="custom.phtml">
    <block type="catalog/product_view_options" template="custom_child.phtml" />
</block>

And inside custom_child.phtml you'll have $this->some_method_from_options_block();

Also you can use a helper to have all your methods in one class.

And when you use child blocks in xml, all you have to do in parents phtml templates is echo $this->getChildHtml('child_name'); (you don't need createBlock) ofc - the parent class has to have the method defined or extend Mage_Core_Block_Abstract (Mage_Core_Block_Template). You should take a look inside folder Mage/Core/Block for more info about the methods some of the core classes have.

Upvotes: 1

Mike
Mike

Reputation: 141

Why won't you make a custom block which extends the catalog/product_view_options block instead?

Upvotes: 0

Related Questions