1

lets say there's the following hierarchy of blocks:

<block name="parent">
    <block name="child" />
</block>

and I want to push a block of my own inside "parent", and put the original "child" as a child of my new block, eg:

<block name="parent">
    <block name="my_block">
        <block name="child" />
    </block>
</block>

how can I do it via layout xml?

I tried something like:

<reference name="parent">
     <block name="my_block">
           <action method="setChild"><name>child</name><block>child</block></action>
     </block>            
</reference>

but it didn't work (I tried calling getChildHtml('child'); ?> inside my template, and got an error.

am I doing it right? how do I update an existing block, add a block of my own, and turn one of the siblings (that were originally in parent) to be a child of my new block?

thanks!

3
  • I believe your example above is actually the valid method for this. Try that and call it via getChildHtml() method, it should work. – Prateek Dec 1 '15 at 13:32
  • I did, its not working. I don't get any errors or log reports, but the template just stop rendering at that line (if for example I have "foo", then getChildHtml(), and then "goo", I will only see "foo" rendered). how can I debug this?? I'm pretty sure its because the child is not really there... thanks – Ronen Ness Dec 1 '15 at 13:44
  • If it's working without your custom block, then it should with your custom block too. Just to make things clear, your method 1 (Without action node) is what I meant with the example. – Prateek Dec 1 '15 at 13:50
0

Use insert or append instead of setChild, otherwise the block is not added to the sortedChildren array.

Also, you create a new block, "my_block", that replaces the old "my_block", instead of referencing the existing block.

Solution

 <reference name="my_block">
       <action method="append"><block>child</block></action>
 </reference>

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.