سؤال

I have a static page in which I call a dynamic block:

{{block type="custommodule/search" name="custommodule_search" template="custommodule/search.phtml" }}

In the layout file of my custom module I'm adding two children of my custommodule_search block:

<layout>
<default module="custommodule">
    <reference name="custommodule_search">
        <block type="custommodule/search_bysize" name="search_by_size" template="custommodule/search/by-size.phtml" />
        <block type="custommodule/search_byvehicle" name="search_by_vehicle" template="custommodule/search/by-vehicle.phtml" />
    </reference>
</default>

All I want is to be able to call these two blocks with this:

$this->getChildHtml('search_by_size')

or this:

$this->getChildHtml('search_by_vehicle')

anywhere in the template of the main dynamic block.

The problem is that they are shown only if the reference is not to "custommodule" but to "content", but this way they are showing in the beginning of the "content" block, not where I want....

هل كانت مفيدة؟

المحلول

Blocks instantiated by the {{block}} directive in can not be referenced from layout XML files.

That is because they are instantiated during the processing of the renderLayout() step, which calls each blocks toHtml() method.

The layout XML is processed before that, during the execution of the loadLayout() step.

To work around this, declare the child blocks without a parent block in the layout XML

<layout>
    <default>
        <block type="custommodule/search_bysize" name="search_by_size" template="custommodule/search/by-size.phtml" />
        <block type="custommodule/search_byvehicle" name="search_by_vehicle" template="custommodule/search/by-vehicle.phtml" />
    </default>
</layout>

Then, in your custommodule/search block template use getBlockHtml() instead of getChildHtml().

<?php echo $this->getBlockHtml('search_by_size') ?>
<?php echo $this->getBlockHtml('search_by_vehicle') ?>

نصائح أخرى

In the template file of your custom block custommodule/search.phtml use the following to show the child blocks and avoid the layout change:

<?php 
    echo Mage::app()->getLayout()->createBlock('custommodule/search_bysize')->setTemplate('custommodule/search/by-size.phtml')->toHtml();
    echo Mage::app()->getLayout()->createBlock('custommodule/search_byvehicle')->setTemplate('custommodule/search/by-vehicle.phtml')->toHtml();
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top