2

I have a template in template/checkout/onepage/hello.phtml

I want to include this phtml file in the success page, i.e template/checkout/success.phtml

So I have created a block in the checkout.xml inside the "checkout_onepage_success" handle as:

<checkout_onepage_success translate="label">
    <label>One Page Checkout Success</label>
    <reference name="root">
        <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
    </reference>
    <reference name="content">
         <block type="core/template" name="onepagehello" template="checkout/onepage/hello.phtml"/>
        <block type="checkout/onepage_success" name="checkout.success" template="checkout/success.phtml"/>
    </reference>
</checkout_onepage_success>  

And this phtml file gets rendered in the success page on the top of the page.

When I write getChildHtml(onepagehello); in success.phtml page, then nothing comes.

  1. I want to know when getChildHtml() works and when the block gets rendered ?
  2. I want to get "hello.phtml" from getChildHtml(), so that I can put this block wherever necessary throughout the page.

2 Answers 2

6

The layout is a hierarchy of blocks and getChildHtml() can only render the children of the current block.

Right now your hierarchy looks like this:

root
+-- content
    +-- onepagehello
    +-- checkout.success

but you need this:

root
+-- content
    +-- checkout.success
        +-- onepagehello

To add a new block to an existing parent block, use <reference>:

<checkout_onepage_success>
    <reference name="checkout.success">
         <block type="core/template" name="onepagehello" template="checkout/onepage/hello.phtml"/>
    </reference>
</checkout_onepage_success>  
3
  • Thank you for the concept @fschmengler.It worked.. I wonder where can I find these simple fundamental concept which makes the base of magento.. Jan 18, 2016 at 11:48
  • For fundamentals of Magento layout I recommend Alan Storms blog at alanstorm.com. You can also find a curated list of Magento resources at github.com/aleron75/mageres Jan 18, 2016 at 12:33
  • what u doing Fabian... you have impress me day by day..what explanation..:)
    – Amit Bera
    Jan 18, 2016 at 19:22
3

Just add element as="NAME" on your block like this

<block type="core/template" name="onepagehello" template="checkout/onepage/hello.phtml" as="hello"/>

you can call like this

getChildHtml('hello');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.