1

I have two custom magento blocks called "exe2" and "example". The exe2 block gets the contents of the example block using the getChildHtml function, but it keeps just returning empty strings to me and only the exe2 contents make it to the screen.

Here is my code my code:

exe2 xml file:

<layout version = "0.1.0">

<test_example_view>


    <block type = "exemplum_mod2/exe2" name = "exemplum.mod2.exe2"  template="exemplum/mod2/exe2.phtml">
        <reference name = "exemplum.mod2.exe2">
            <block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />
        </reference>
    </block>


</test_example_view>

example xml file

<layout version="0.1.0">
<default>

    <block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />        

</default>

Here is the exe2 phtml file that has the getChildHtml call in it:

<h1> 2nd </h1>
<?php

    echo $this->getMessage();
    echo $this->getChildHtml("exemplum.mod2.exe2");

?>

hello

example.phtml file:

<h1>Hello there</h1>

And finally heres the controller file that loads the blocks:

<?php



class exemplum_example1_ExampleController extends Mage_Core_Controller_Front_Action{


    public function viewAction(){

        $this->loadLayout();

        $block = $this->getLayout()->createBlock('exemplum_mod2/exe2');
        $block->setTemplate("exemplum/mod2/exe2.phtml");


        $this->getLayout()->getBlock('content')->append($block);

        $this->renderLayout();

    }



}

What is the correct way to add one block as the child block of a another block in the xml file? Every solution that i found on google didn't seem to work in my case so what am i doing wrong here?

0

I guess that the code below should work for you:

exe2.xml:

...

<block type = "exemplum_mod2/exe2" name = "exemplum.mod2.exe2"  template="exemplum/mod2/exe2.phtml">
    <block type="exemplum/example" name="exemplum.example" template="exemplum/example1/example.phtml" />
</block>

...

exe2.phtml:

...

echo $this->getChildHtml("exemplum.example");

...

ExampleController.php:

...

public function viewAction(){

    $this->loadLayout();

    $block = $this->getLayout()->getBlock('exemplum.mod2.exe2');

    $this->getLayout()->getBlock('content')->append($block);

    $this->renderLayout();

}

...
  • This didn't work for me, but if i created the example child block seperatly and added it as a child block of the exe2 block it started working. does this mean that createBlock does not load the child blocks of the block or am i still doing something wrong? – Steven doe Oct 31 '16 at 15:08

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.