9

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 ?>
...
asked Jun 2, 2014 at 14:11

5 Answers 5

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

answered Jun 3, 2014 at 5:59
5

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..

answered Jan 13, 2015 at 18:58
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')
Anil Suthar
4,7311 gold badge16 silver badges22 bronze badges
answered Aug 27, 2015 at 22:26
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>
answered Jun 2, 2014 at 14:26
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. Commented Jun 2, 2014 at 15:12
  • 2
    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.. Commented Jun 2, 2014 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 Commented Jun 2, 2014 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>
answered Jun 2, 2014 at 15:08
3
  • 1
    Referring to content loads my block, but I can't control where the block gets placed if I refer to `content. Commented Jun 2, 2014 at 15:11
  • It gets placed by <?php echo $this->getChildHtml('abc') ?> Commented Jun 2, 2014 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. Commented Jun 2, 2014 at 15:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.