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 ?>
...
5 Answers 5
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
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..
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')
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>
-
I tried it, but it still doesn't work. The strange thing is that I have other updates in
my layout.xmlthat uses the same method, and they all work.laketuna– laketuna2014年06月02日 15:12:26 +00:00Commented Jun 2, 2014 at 15:12 -
2I found out why. My extension loads first, and the extension that loads
onestepcheckout_index_indexis loaded after mine. Therefore, my layout update was referring to a handle that doesn't exist yet..laketuna– laketuna2014年06月02日 15:37:06 +00:00Commented 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, thankspzirkind– pzirkind2014年06月02日 17:33:40 +00:00Commented Jun 2, 2014 at 17:33
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>
-
1Referring to
contentloads my block, but I can't control where the block gets placed if I refer to `content.laketuna– laketuna2014年06月02日 15:11:28 +00:00Commented Jun 2, 2014 at 15:11 -
It gets placed by
<?php echo $this->getChildHtml('abc') ?>Pronto– Pronto2014年06月02日 15:16:11 +00:00Commented Jun 2, 2014 at 15:16 -
I need to place this block in a specific location. As far as I know, referencing
contentplaces the block automatically andecho $this->getChildHtml('abc')has no effect. Correct me if I'm wrong.laketuna– laketuna2014年06月02日 15:23:14 +00:00Commented Jun 2, 2014 at 15:23