By Engine23

Magento - Adding to existing getChildHtml elements using local.xml

So, ironically in the past 2 days, I was asked to perform the same task (on two separate projects!)

Basically, in both requests, there is a current template that has getChildHtml and instead of adding a new one to include a new template file, I wanted to be able to just append my to the existing one.

Several benefits, the main one being I am not over-writting a core template file that may change during upgrades...and there is existing placeholders on the template that I can add my custom data inside.

This one that I will be showing is the customer accout create form.

That form had all the elements I wanted, except in this case they wanted to add some extra content.

Now, yes,  I could of just got the main template, app/frontend/base/default/template/persistent/customer/form/register.phtml and put it in my theme, and just hard coded my new content.  BUT that is not the magento way, that does not allow for fallback, and that means that if we switch themes I need to remember to move that code, and hope that things dont change or break in the future.

The better way, just add your new section via xml!

I struggled with the exact syntax until I remembered I did a similar thing when I added captcha to a magento website earlier.

Basically to make it work, you have to set your block inside the "content" reference.  Then, inside your block, your next action is to say, hey, using core/text_list ( which automatically renders its children ) you say what existing getChildHtml() you want to insert your new block.

Here is an example of the customer account create adding new content to the existing getChildHtml('form_fields_before')

        <reference name="customer_form_register">
            <reference name="content">
                <block type="core/text_list" name="form_fields_before">
                    <block type="core/template" name="dpc.register.questions" template="customer/form/dpc_register_questions.phtml" />
            <action method="setShowAddressFields"><value>true</value></action>