Magento: Overriding Template File from Custom Module

Scenario:

I have created a new custom module. Now, I need to edit a default template file.

For example:-

– I have created a new custom module that adds some functionality to Customer Account section on frontend.
– Now, I need to edit the Customer Account Edit template (My Account -> Account Information).
– This template is present in template/customer/form/edit.phtml

I could simply edit this template. But this can result error when the custom module which I have created is disabled.

Therefore, a better solution will be to override the template file. That is to set a new custom template instead of the default one.

Here is how we do it:

This is the default code present in layout/customer.xml (We are not going to edit this)


<customer_account_edit translate="label">
	<label>Customer Account Edit Form</label>
	<update handle="customer_account"/>
	<reference name="root">
		<action method="setHeaderTitle" translate="title" module="customer"><title>Edit Account Info</title></action>
	</reference>
	<reference name="content">
		<block type="customer/form_edit" name="customer_edit" template="customer/form/edit.phtml"/>
	</reference>

	<reference name="left">
	  <action method="unsetChild"><name>left.permanent.callout</name></action>
	</reference>
</customer_account_edit>

We just need to set a different template for the block named ‘customer_edit‘ present in the above XML.

Here is our custom module’s XML. This file/code does all the magic for us:


<?xml version="1.0"?>
<layout version="0.1.0"> 	
	<!-- Overriding Template File -->	
	<customer_account_edit>
		<reference name="customer_edit">
			<action method="setTemplate">                
				<template>mymodule/customer/form/edit.phtml</template>
			</action>
		</reference>
	</customer_account_edit>	
</layout>

In the XML above, we have set a new template for ‘customer_edit‘ block.

Now, when we go to Customer Account Information section (My Account -> Account Information), our custom module’s template file “mymodule/customer/form/edit.phtml” will be loaded instead of the default template file “customer/form/edit.phtml“.

More about this: Magento Extension Needs to Override a Template

Hope this helps. Thanks.