Magento: Add a custom block inside another block

You can add custom blocks inside already existing block through your custom layout xml file. You don’t need to change the template (phtml) code.

Suppose, you want to add a block inside product view page. To do so, you can write the following xml code in your custom module’s layout file. In the below code, I have added a button in product view page.

Note: output=”toHtml” is required to print the button.


<catalog_product_view>
	<reference name="content">
		<reference name="product.info">			
			<block type="mymodule/myblock" name="mymodule.product.info.button" as="mybutton" template="mymodule/mybutton.phtml" output="toHtml" before="-"/>
		</reference>
	</reference>
</catalog_product_view>

Another example: Below code will add the custom button after Add to Cart button in product view page.


<catalog_product_view>
	<reference name="content">
		<reference name="product.info">
			<reference name="product.info.addtocart">
				<block type="mymodule/myblock" name="mymodule.product.info.button" as="mybutton" template="mymodule/mybutton.phtml" />
			</reference>
		</reference>
	</reference>
</catalog_product_view>

Hope this helps. Thanks.