8

I'm having a problem where I can't seem to add a child block in a layout XML file. What am I doing wrong in mylayout.xml that I can't load abc? I have the following files.

onestepcheckout.xml

<onestepcheckout_index_index>
    ...
    <reference name="content">
        <block type="onestepcheckout/checkout" name="onestepcheckout.checkout" template="onestepcheckout/checkout.phtml">
            ...
            <!-- this child block can be loaded -->
             <block type="block/class" template="path/to/template/template.phtml" name="qwe" as="qwe" />
            ...
        </block>
    </reference>
    ...
</onestepcheckout_index_index>

mylayout.xml

<onestepcheckout_index_index>
    <reference name="onestepcheckout.checkout">
        <!-- this child block can not be loaded -->
        <block type="block/class" template="path/to/template/template.phtml" name="abc" as="abc" />
    </reference>
</onestepcheckout_index_index>

checkout.phtml

...
<?php echo $this->getChildHtml('abc') // doesn't work ?>
<?php echo $this->getChildHtml('qwer') // works ?>
...
8

If its a module loading issue, I think you should add a tag in your module declaration file. like below

<depends> <companyname_modulename/> </depends>

This will insure that your module will be loaded after companyname_modulename

4

I found out why. My extension loads first, and the extension that loads onestepcheckout_index_index is loaded after mine. Therefore, my layout update was referring to a handle that doesn't exist yet..

4

Just to clarify this. Several steps were necessary. First, declare the block as a child block.

<action method="setChild"><alias>my_name</alias><child>my.name</child></action>

Then add the dependency to your module declaration file. In my case it was Idev_OneStepCheckout

<depends>
    <Idev_OneStepCheckout />
</depends>

Finally, in the template it will only work if you use the alias.

echo $this->getChildHtml('my_name')
2

Not sure why that doesn't work either. Have you tried explicitly declaring the block as a child?

<onestepcheckout_index_index>
    <reference name="onestepcheckout.checkout">
        <!-- this child block can not be loaded -->
        <block type="block/class" template="path/to/template/template.phtml" name="abc" as="abc" />
        <action method="setChild"><child>abc</child><alias>abc</alias></action>
    </reference>
</onestepcheckout_index_index>
3
  • I tried it, but it still doesn't work. The strange thing is that I have other updates in my layout.xml that uses the same method, and they all work. Jun 2 '14 at 15:12
  • 1
    I found out why. My extension loads first, and the extension that loads onestepcheckout_index_index is loaded after mine. Therefore, my layout update was referring to a handle that doesn't exist yet.. Jun 2 '14 at 15:37
  • @laketuna that's very interesting, i had a similar problem recently and just used an observer, but this question sparked my curiosity again, thanks
    – pzirkind
    Jun 2 '14 at 17:33
1

If you want to add new block, you should reference structural blocks, not content blocks. Try out something like this instead:

<onestepcheckout_index_index>
    <reference name="content">
        <block type="block/class" template="path/to/template/template.phtml" name="abc" as="abc" />
    </reference>
</onestepcheckout_index_index>
3
  • 1
    Referring to content loads my block, but I can't control where the block gets placed if I refer to `content. Jun 2 '14 at 15:11
  • It gets placed by <?php echo $this->getChildHtml('abc') ?>
    – Pronto
    Jun 2 '14 at 15:16
  • I need to place this block in a specific location. As far as I know, referencing content places the block automatically and echo $this->getChildHtml('abc') has no effect. Correct me if I'm wrong. Jun 2 '14 at 15:23

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.